Skip to content
synccn

JSON Viewer

New

Collapsible JSON tree with type colours, search that opens matching branches, keyboard navigation and copy for paths and values.

event: {
id: "evt_1Q8xRk"
type: "invoice.paid"
livemode: false
created: 1795680000
data: {
amount: 4800
currency: "usd"
request: {
id: "req_93kd"
idempotency-key: "8b1f…c2"
event

Focused root · use the arrow keys to move

Installation

$ pnpm dlx shadcn@latest add @synccn/json-viewer

First 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
    unknown

    The value to show. Anything JSON.stringify accepts; dates and undefined are shown as strings.

  • defaultExpandDepth
    number

    Default 1

    Levels open at first. 0 shows only the root, Infinity opens everything.

  • rootName
    string

    Default "root"

    Label for the root row.

  • searchable
    boolean

    Default true

    Shows a search box that highlights matching keys and values and opens their parents.

  • onSelect
    (path: string, value: unknown) => void

    Called when a row is focused, with its path (for example `user.roles[0]`) and value.

  • maxHeight
    number

    Default 360

    Maximum height before the tree scrolls, in pixels.

  • className
    string

    Extra 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

npm packages

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