What is react-json-tree?
The react-json-tree package is a React component for efficiently rendering a JSON tree structure. It is useful for displaying JSON data in a hierarchical format, making it easier to navigate and understand complex JSON objects.
What are react-json-tree's main functionalities?
Basic JSON Tree Rendering
This feature allows you to render a basic JSON tree structure. The JSONTree component takes a JSON object as a prop and displays it in a hierarchical format.
import React from 'react';
import JSONTree from 'react-json-tree';
const data = {
name: 'John',
age: 30,
city: 'New York'
};
const App = () => (
<div>
<h1>JSON Tree</h1>
<JSONTree data={data} />
</div>
);
export default App;
Custom Theme
This feature allows you to apply a custom theme to the JSON tree. You can define a theme object and pass it as a prop to the JSONTree component to customize the appearance of the tree.
import React from 'react';
import JSONTree from 'react-json-tree';
const data = {
name: 'John',
age: 30,
city: 'New York'
};
const theme = {
scheme: 'monokai',
author: 'wimer hazenberg (http://www.monokai.nl)',
base00: '#272822',
base01: '#383830',
base02: '#49483e',
base03: '#75715e',
base04: '#a59f85',
base05: '#f8f8f2',
base06: '#f5f4f1',
base07: '#f9f8f5',
base08: '#f92672',
base09: '#fd971f',
base0A: '#f4bf75',
base0B: '#a6e22e',
base0C: '#a1efe4',
base0D: '#66d9ef',
base0E: '#ae81ff',
base0F: '#cc6633'
};
const App = () => (
<div>
<h1>JSON Tree with Custom Theme</h1>
<JSONTree data={data} theme={theme} />
</div>
);
export default App;
Collapsible Nodes
This feature allows you to make the nodes of the JSON tree collapsible. By default, all nodes are expanded, but you can control the expansion state by using the shouldExpandNode prop.
import React from 'react';
import JSONTree from 'react-json-tree';
const data = {
name: 'John',
age: 30,
address: {
city: 'New York',
zip: '10001'
}
};
const App = () => (
<div>
<h1>Collapsible JSON Tree</h1>
<JSONTree data={data} shouldExpandNode={() => false} />
</div>
);
export default App;
Other packages similar to react-json-tree
react-json-view
react-json-view is a React component for displaying and editing JSON data. It provides a more interactive experience compared to react-json-tree, allowing users to edit JSON values directly within the tree structure. It also supports features like collapsing/expanding nodes, searching, and custom themes.
jsoneditor-react
jsoneditor-react is a wrapper around the JSONEditor library for use in React applications. It offers a rich set of features for viewing, editing, and manipulating JSON data, including a tree view, code view, and text view. It is more feature-rich compared to react-json-tree, providing advanced functionalities like schema validation and history management.
react-json-inspector
react-json-inspector is a React component for inspecting JSON data. It provides a simple and efficient way to navigate through large JSON objects. Unlike react-json-tree, it focuses more on search and filtering capabilities, making it easier to find specific data within a large JSON structure.
react-json-tree
React JSON Viewer Component, Extracted from redux-devtools. Supports iterable objects, such as Immutable.js.
Usage
import { JSONTree } from 'react-json-tree';
import { Map } from 'immutable';
const json = {
array: [1, 2, 3],
bool: true,
object: {
foo: 'bar',
},
immutable: Map({ key: 'value' }),
};
<JSONTree data={json} />;
Result:
Check out examples directory for more details.
Theming
This component now uses react-base16-styling module, which allows to customize component via theme
property, which can be the following:
- base16 theme data. The example theme data can be found here.
- object that contains style objects, strings (that treated as classnames) or functions. A function is used to extend its first argument
{ style, className }
and should return an object with the same structure. Other arguments depend on particular context (and should be described here). See createStylingFromTheme.js for the list of styling object keys. Also, this object can extend base16
theme via extend
property.
Every theme has a light version, which is enabled with invertTheme
prop.
const theme = {
scheme: 'monokai',
author: 'wimer hazenberg (http://www.monokai.nl)',
base00: '#272822',
base01: '#383830',
base02: '#49483e',
base03: '#75715e',
base04: '#a59f85',
base05: '#f8f8f2',
base06: '#f5f4f1',
base07: '#f9f8f5',
base08: '#f92672',
base09: '#fd971f',
base0A: '#f4bf75',
base0B: '#a6e22e',
base0C: '#a1efe4',
base0D: '#66d9ef',
base0E: '#ae81ff',
base0F: '#cc6633',
};
<div>
<JSONTree data={data} theme={theme} invertTheme={false} />
</div>;
Result (Monokai theme, dark background):
Advanced Customization
<div>
<JSONTree
data={data}
theme={{
extend: theme,
// underline keys for literal values
valueLabel: {
textDecoration: 'underline',
},
// switch key for objects to uppercase when object is expanded.
// `nestedNodeLabel` receives additional argument `expandable`
nestedNodeLabel: ({ style }, keyPath, nodeType, expanded) => ({
style: {
...style,
textTransform: expanded ? 'uppercase' : style.textTransform,
},
}),
}}
/>
</div>
Customize Labels for Arrays, Objects, and Iterables
You can pass getItemString
to customize the way arrays, objects, and iterable nodes are displayed (optional).
By default, it'll be:
<JSONTree getItemString={(type, data, itemType, itemString, keyPath)
=> <span>{itemType} {itemString}</span>}
But if you pass the following:
const getItemString = (type, data, itemType, itemString, keyPath)
=> (<span> // {type}</span>);
Then the preview of child elements now look like this:
Customize Rendering
You can pass the following properties to customize rendered labels and values:
<JSONTree
labelRenderer={([key]) => <strong>{key}</strong>}
valueRenderer={(raw) => <em>{raw}</em>}
/>
In this example the label and value will be rendered with <strong>
and <em>
wrappers respectively.
For labelRenderer
, you can provide a full path - see this PR.
Their full signatures are:
labelRenderer: function(keyPath, nodeType, expanded, expandable)
valueRenderer: function(valueAsString, value, ...keyPath)
More Options
shouldExpandNodeInitially: function(keyPath, data, level)
- determines if node should be expanded when it first renders (root is expanded by default)hideRoot: boolean
- if true
, the root node is hidden.sortObjectKeys: boolean | function(a, b)
- sorts object keys with compare function (optional). Isn't applied to iterable maps like Immutable.Map
.postprocessValue: function(value)
- maps value
to a new value
isCustomNode: function(value)
- overrides the default object type detection and renders the value as a single valuecollectionLimit: number
- sets the number of nodes that will be rendered in a collection before rendering them in collapsed rangeskeyPath: (string | number)[]
- overrides the initial key path for the root node (defaults to [root]
)
Credits
Similar Libraries
License
MIT