Xzero UI
Pro library

JSON viewer

A collapsible JSON tree with search, paging for long arrays, and copy value or path.

$ pnpm dlx shadcn@4.23.0 add @xzero-ui/json-viewer
Live · keyboard ready

When to use

When not to use

Installation

Register both Xzero UI namespaces in your project's components.json:

{
  "registries": {
    "@xzero-ui": "https://mcp.xzero.co.uk/r/{name}.json",
    "@xzero-ui-pro": "https://mcp.xzero.co.uk/r/pro/{name}.json"
  }
}
npx shadcn@4.23.0 add @xzero-ui/json-viewer

Install xzero-foundation once, and import @/components/xzero/foundation.css in your app root. No Xzero token is used by this snapshot. For sample portraits, photos, fonts, or logos, download https://mcp.xzero.co.uk/install-assets.mjs and run node xzero-install-assets.mjs <receiving-project>.

Usage

import { JsonViewer } from "@/components/xzero/json-viewer/json-viewer";

export function WebhookPayload({ payload }: { payload: unknown }) {
  return (
    <JsonViewer
      data={payload}
      rootName="event"
      defaultExpandDepth={2}
      maxHeight={360}
      onCopy={({ kind }) => toast(kind === "path" ? "Path copied" : "Value copied")}
    />
  );
}

API reference

JsonViewer

A collapsible JSON tree with type colors, search that opens matching branches, paged long branches, and copy value or path per row.

PropTypeDefaultDescription
data (required)unknown–Any JSON-like value.
rootNamestring"root"Name of the root in paths, as in root.users[0].name.
defaultExpandDepthnumber1Levels open on first render when defaultExpanded is not set. 1 opens the root.
expandedstring[]–Controlled paths of open branches.
defaultExpandedstring[]–Open branch paths on first render when uncontrolled.
onExpandedChange(paths: string[]) => void–Called when branches open or close.
searchablebooleantrueShows the search field.
querystring–Controlled search text.
defaultQuerystring""Starting search text when uncontrolled.
onQueryChange(query: string) => void–Called as the search text changes.
pageSizenumber50Children shown per page in a long array or object.
copyablebooleantrueShows copy value and copy path actions.
onCopy(detail: JsonViewerCopyDetail) => void–Called after a copy with { kind: "value" | "path", path, text }.
onSelect(detail: { path: string; value: unknown; type: JsonValueType }) => void–Called when a row becomes the current one.
showPathbooleantrueShows the current row's path under the tree.
maxHeightnumber | string420Height of the scrolling tree area. Numbers are pixels.
labelstring"JSON"Accessible name of the tree.
classNamestring–Class on the root.
refRef<HTMLDivElement>–The root element.

Keyboard interactions

KeysAction
ArrowDown / ArrowUpMoves to the next or previous visible row.
ArrowRightOpens a closed branch, or moves into an open one.
ArrowLeftCloses an open branch, or moves to the parent.
Home / EndJumps to the first or last visible row.
Enter / SpaceToggles a branch, or loads the next page on a Show more row.
Cmd/Ctrl + CCopies the focused row's value; add Shift to copy its path. Ignored while text is selected.
/Focuses the search field.
Enter / Shift + Enter (in search)Jumps to the next or previous match.
Escape (in search)Clears the query.
ArrowDown (in search)Moves focus into the tree.

Accessibility

Motion

Responsive behavior

Performance

Notes for AI

Also in tables

Guidance for AI tools

JSON viewer: A collapsible JSON tree with search, paging for long arrays, and copy value or path. Follow the declared prop types and do not invent props. Keep keyboard access, reduced motion support, and both light and dark themes intact when adapting it.

Full library index: https://ui.xzero.co.uk/llms.txt

Browse all components