Skip to content
ContentJSON viewerMenu

JSON viewer

A collapsible, searchable tree for tool inputs and outputs, with copy path and copy value on every row and full keyboard navigation.

A pull request from a GitHub tool call, with copy path and copy value on every row.

Installation

$ pnpm dlx shadcn@latest add https://chatcn.dev/r/json-viewer.json

Usage

tsx
import { JsonViewer } from "@/components/ui/chatcn/json-viewer"

<JsonViewer data={part.output} defaultExpandDepth={1} label="Tool output" />

Paths copy as JSONPath, for example $.labels[0].name. Strings longer than maxStringLength are cut with a control that shows the rest, and containers with more than pageSize children page in.

Examples

Search matches keys and values, highlights every hit and opens the path to each one. Enter and Shift+Enter step through matches.

Search highlights matches and opens the path to each one.

Props

PropTypeNotes
dataunknownAny JSON value
defaultExpandDepthnumberContainers shallower than this start open. Default 1
defaultQuerystringInitial search
toolbarbooleanSearch, expand all and copy JSON. Default true
maxStringLengthnumberDefault 120
pageSizenumberChildren per container before Show more. Default 100
maxHeightnumber | stringDefault 360
labelstringAccessible name of the tree

Keyboard

KeyAction
Up, DownPrevious and next row
RightOpen a container, or move to its first child
LeftClose a container, or move to its parent
Home, EndFirst and last row
Enter, SpaceToggle a container, or show a whole long string
CCopy the value of the focused row
PCopy the path of the focused row
Enter in searchNext match. Shift+Enter for the previous one
Esc in searchClear the search

Accessibility

  • The tree follows the APG tree view pattern: role=tree, treeitem rows with level, position and aria-expanded, and one roving tab stop.
  • Copying from the keyboard is announced in a polite live region, including the path that was copied.
  • Types get subtle hues (strings green, numbers blue, booleans purple, null gray), and strings keep their quotes and literals read as themselves, so nothing depends on color.

Motion

  • None. Opening and closing rows is frequent and keyboard-driven, so it is instant.
  • Row actions appear on hover or focus without a transition.