JsonViewer

An interactive tree to read JSON, with nodes that open and close. Use it to inspect a response, a config or any value you would otherwise print with JSON.stringify.

General

Click a node, or use the arrow keys, to open it. rootName names the tree.

Controls and copy

withControls adds Expand all and Collapse all, and withCopyButton copies the whole JSON.

Depth and selection

defaultExpandDepth opens more levels at the start. onValueSelect runs when a value is clicked or chosen with Enter.

APIs

JsonViewer.Props

AttributeDescriptionTypeAccepted valuesDefault
valuethe data to showunknown--
defaultExpandDepthlevels that start open, the root always doesnumber-1
allExpandedopen every nodeboolean-false
rootNamename of the tree, written before the root valuestring--
sortKeyssort the keys of the objectsboolean-false
maxDisplayLengthchildren shown before a row says how many are leftnumber-100
withControlsshow Expand all and Collapse allboolean-false
withCopyButtonshow a button that copies the JSONboolean-false
copyLabeltext of the copy buttonstring-Copy JSON
copiedLabeltext of the copy button after it copiedstring-Copied
expandAllLabeltext of the expand all buttonstring-Expand all
collapseAllLabeltext of the collapse all buttonstring-Collapse all
onValueSelectcalled when a value, not a container, is chosen(path: JsonPath, value: unknown) => void--
...native props, passed to the rootHTMLAttributes'aria-label', 'className', ...-

JsonPath

type JsonPath = Array<string | number> // keys and indexes, from the root

Ref

The ref points at the root div.

Accessibility

The viewer is a tree, named by rootName, aria-label or JSON. Each row is a treeitem with its level, position and, for a container, whether it is open. Only one row is in the tab order.

KeyAction
Arrow Down and UpNext and previous row
Arrow RightOpen the node, or go to its first child
Arrow LeftClose the node, or go to its parent
Home and EndFirst and last row
Enter and SpaceOpen or close a container, or choose a value
Edit this page on GitHub