React JSON View Component

1.21.3 · maintenance · verified Sun Apr 19

React JSON View (RJV) is an interactive React component designed for displaying and optionally editing JavaScript arrays and JSON objects within web applications. The version provided (1.21.3) from the `mac-s-g/react-json-view` repository has announced it is no longer actively maintained. However, an actively maintained fork exists at `@microlink/react-json-view`, which offers ongoing updates, performance improvements, and additional features, including full TypeScript support and a redesigned API in its v2. The original `mac-s-g` version is transpiled to ES5, ensuring broad compatibility. Key differentiators of both versions include extensive theme support (base-16), customizable expand/collapse behavior, optional editing features via callbacks, clipboard functionality, and various display options for data types and object sizes.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to integrate `ReactJson` into a functional React component, displaying a sample JSON object with various data types. It shows how to enable editing, adding, and deleting properties using the `onEdit`, `onAdd`, and `onDelete` callbacks, and configures several display options like themes, collapse depth, and data types.

import React, { useState } from 'react';
import ReactJson from 'react-json-view';

function MyJsonViewer() {
  const [jsonData, setJsonData] = useState({
    id: 1,
    name: 'Sample Product',
    price: 29.99,
    inStock: true,
    tags: ['electronics', 'gadget', 'sale'],
    details: {
      manufacturer: 'TechCorp',
      weight: '1.5 kg',
      dimensions: { width: '10cm', height: '5cm', depth: '2cm' }
    },
    reviews: [
      { user: 'Alice', rating: 5, comment: 'Great product!' },
      { user: 'Bob', rating: 4, comment: 'Good value for money.' }
    ],
    lastUpdated: new Date().toISOString()
  });

  const handleEdit = (edit) => {
    console.log('JSON Edited:', edit);
    // In a real app, you would typically update your state or send to an API
    // For this example, we'll update the local state if onEdit is used.
    setJsonData(edit.updated_src);
  };

  return (
    <div style={{ fontFamily: 'sans-serif', padding: '20px' }}>
      <h2>Interactive JSON Viewer</h2>
      <p>This component displays and allows modification of JSON objects.</p>
      <ReactJson
        src={jsonData}
        name="productData"
        theme="monokai"
        iconStyle="triangle"
        collapsed={1} // Collapse objects at depth 1 by default
        enableClipboard={true}
        displayObjectSize={true}
        displayDataTypes={false}
        onEdit={handleEdit}
        onAdd={handleEdit} // Enable add functionality
        onDelete={handleEdit} // Enable delete functionality
        indentWidth={4}
        groupArraysAfterLength={5}
      />
      <div style={{ marginTop: '20px', backgroundColor: '#f0f0f0', padding: '15px' }}>
        <h3>Current State (JSON.stringify):</h3>
        <pre>{JSON.stringify(jsonData, null, 2)}</pre>
      </div>
    </div>
  );
}

export default MyJsonViewer;

view raw JSON →