JsonViewer
Explore JSON as a collapsible tree: search keys and values, expand to any depth, copy a value or its JSONPath, or read the raw text.
Status: beta. Category: Viewers.
import { JsonViewer } from '@glyphui/viewers/json'
Exports: JsonViewer, jsonViewerStyles, indexJson, parseJson, searchJson, childPath.
Notes
- No extra packages needed.
- The tree is keyboard navigable: arrow keys move and expand, Home and End jump, Enter toggles.
- Only rows in view are rendered, so documents with tens of thousands of nodes stay responsive.
- Select a row to see its JSONPath in the footer, with buttons to copy the path or the value.
JsonViewer props
| Prop | Type | Default | Description |
|---|---|---|---|
bordered | boolean | Draw the viewer as a bordered card. Default true. | |
className | string | ||
content | string | JSON text, instead of source. | |
data | unknown | An already parsed value, instead of source or content. | |
defaultExpandDepth | number | Levels expanded at first. Default 2. Use Infinity to expand everything. | |
height | string | number | Height of the viewer. Default 420. | |
label | string | Accessible name for the tree. Default "JSON". | |
source | ViewerSource | A JSON file: a URL, a File/Blob, or bytes. |
Example: A JSON file
Search for “invoice”, then step through the matches.
import { JsonViewer } from '@glyphui/viewers/json'
import { fixture } from '../../../lib/fixtures'
export default function Example() {
return <JsonViewer source={fixture('workspace.json')} label="workspace.json" className="w-full" />
}
Example: A value from your app
Pass parsed data directly, here an API response.
import { JsonViewer } from '@glyphui/viewers/json'
const response = {
data: [
{ id: 'evt_1', type: 'invoice.paid', amount: 1250, livemode: true },
{ id: 'evt_2', type: 'invoice.overdue', amount: 86.5, livemode: true },
],
has_more: false,
next_cursor: null,
}
export default function Example() {
return (
<JsonViewer
data={response}
defaultExpandDepth={3}
height={300}
label="Events response"
className="w-full"
/>
)
}
Example: Invalid JSON
Syntax errors point at the line and column.
import { JsonViewer } from '@glyphui/viewers/json'
const broken = `{
"name": "Ledgerline",
"plan": "growth",
"seats": 25,
}`
export default function Example() {
return <JsonViewer content={broken} height={260} className="w-full" />
}
Live page: https://glyphui.xyz/components/json-viewer