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

PropTypeDefaultDescription
borderedbooleanDraw the viewer as a bordered card. Default true.
classNamestring
contentstringJSON text, instead of source.
dataunknownAn already parsed value, instead of source or content.
defaultExpandDepthnumberLevels expanded at first. Default 2. Use Infinity to expand everything.
heightstring | numberHeight of the viewer. Default 420.
labelstringAccessible name for the tree. Default "JSON".
sourceViewerSourceA 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