React JSON Editor Component

1.29.0 · active · verified Sun Apr 19

json-edit-react is a highly configurable React component designed for both viewing and interactively editing JSON or arbitrary JavaScript object data. It provides features like inline editing, granular control over edit/delete/add permissions per element, optional JSON Schema validation (with a third-party library), and extensive UI customization through themes and CSS. The package, currently at version 1.29.0, receives frequent minor and patch updates, indicating active maintenance and ongoing feature development. Key differentiators include its self-contained nature (minimal external UI dependencies), search and filtering capabilities, support for custom components to render specific node types, localization, and drag-and-drop re-ordering. It offers a comprehensive solution for applications requiring dynamic JSON data manipulation within a React environment.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates rendering an editable JSON structure, updating state, applying a built-in theme, and configuring basic editing restrictions using a functional React component.

import React, { useState, useCallback } from 'react';
import { JsonEditor } from 'json-edit-react';
// Import a built-in theme (since v1.19.0)
import { darkTheme } from 'json-edit-react/themes';

function MyJsonEditorComponent() {
  const [jsonData, setJsonData] = useState({
    user: {
      id: 1,
      name: "Alice Smith",
      email: "alice@example.com",
      isActive: true,
      roles: ['admin', 'editor'],
      settings: {
        theme: 'dark',
        notifications: true,
        dataLimit: 1000
      }
    },
    products: [
      { id: 'a1', name: 'Laptop', price: 1200 },
      { id: 'b2', name: 'Mouse', price: 25 }
    ]
  });

  // Callback to handle changes from the editor
  const handleDataChange = useCallback((updatedData) => {
    console.log('JSON data updated:', updatedData);
    setJsonData(updatedData);
  }, []);

  return (
    <div style={{ maxWidth: '800px', margin: '20px auto', padding: '20px', border: '1px solid #ccc', borderRadius: '8px' }}>
      <h2>Interactive JSON Data</h2>
      <JsonEditor
        data={jsonData}
        setData={handleDataChange}
        restrictAdd={(path, value) => path.length < 3} // Example: only allow adding at root or first level
        restrictDelete={(path) => path[0] !== 'products'} // Example: prevent deleting top-level 'products'
        rootName="MyAppData"
        theme={darkTheme}
        highlightUpdates={true}
        showArrayIndices={true}
        showObjectSize={true}
      />
      <pre style={{ marginTop: '20px', backgroundColor: '#eee', padding: '10px', borderRadius: '4px' }}>
        <code>{JSON.stringify(jsonData, null, 2)}</code>
      </pre>
    </div>
  );
}

export default MyJsonEditorComponent;

view raw JSON →