JSON Viewer
NewCollapsible JSON tree with type colours, search that opens matching branches, keyboard navigation and copy for paths and values.
eventFocused root · use the arrow keys to move
Installation
$ pnpm dlx shadcn@latest add @synccn/json-viewerFirst time in this project? Connect synccn with one command.
Usage
import { JsonViewer } from "@/components/ui/json-viewer";<JsonViewer data={response} defaultExpandDepth={2} onSelect={(path) => …} />API reference
Generated from the component's TypeScript types, so it always matches the code.
JsonViewer
Collapsible JSON tree with type colours, search, keyboard navigation and copy for paths and values. Paths are written the way you'd read them in code: `user.roles[0]`, `headers["content-type"]`.
- datarequired
unknownThe value to show. Anything JSON.stringify accepts; dates and undefined are shown as strings.
- defaultExpandDepth
numberDefault
1Levels open at first. 0 shows only the root, Infinity opens everything.
- rootName
stringDefault
"root"Label for the root row.
- searchable
booleanDefault
trueShows a search box that highlights matching keys and values and opens their parents.
- onSelect
(path: string, value: unknown) => voidCalled when a row is focused, with its path (for example `user.roles[0]`) and value.
- maxHeight
numberDefault
360Maximum height before the tree scrolls, in pixels.
- className
stringExtra classes for the root element.
Dependencies
Read from the component's imports when the site builds. The CLI installs all of these for you.
Helpers
- lib/clipboardcopyText (copies to the clipboard)
- lib/utilscn (merges class names)
npm packages
- clsx for cn()
- tailwind-merge for cn()
- lucide-react for Check, ChevronRight, ChevronsDownUp, ChevronsUpDown, Copy, Search
Keyboard
- ↓or↑Move between rows
- →Open a branch, or move into it
- ←Close a branch, or move to its parent
- HomeorEndFirst or last row
- EnterorSpaceOpen or close a branch
Accessibility
- Uses tree and treeitem roles with aria-level, aria-setsize, aria-posinset and aria-expanded.
- One row is in the tab order at a time; the search reports its match count politely.
From the source
Read from this component's code when the site builds, so it can't drift.
- ARIA roles: tree, treeitem
- ARIA attributes: aria-expanded, aria-label, aria-level, aria-live, aria-posinset, aria-selected, aria-setsize
- Keys handled: ArrowUp, ArrowDown, ArrowLeft, ArrowRight, Home, End, Enter, Space
- Shows a visible focus ring for keyboard users
- Labels are tied to their controls
- Announces changes to screen readers