JSON viewer

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

npx shadcn@latest add @silicon-ui/json-viewer
datanew
Loading…

Installation

With the shadcn CLI

npx shadcn@latest add @silicon-ui/json-viewer

Manually

  1. Install the dependencies.

    npm install lucide-react motion
  2. Copy these files into your project. Shared files are skipped if you already have them.

    • components/silicon-ui/json-viewer/json-viewer.tsx
    • components/silicon-ui/json-viewer/json-viewer.module.css
  3. Install the foundation once per app for the design and motion tokens: npx shadcn@latest add @silicon-ui/silicon-foundation

Usage

Source codetsx
import { JsonViewer } from "@/components/silicon-ui/json-viewer/json-viewer";

API reference

PropTypeDefault
data*unknown—
rootName

Name of the root in paths, as in `root.users[0].name`. Defaults to "root".

string"root"
defaultExpandDepth

Levels open on first render when `defaultExpanded` is not set. Defaults to 1, which opens the root.

number1
expanded

Paths of open branches.

string[]—
defaultExpandedstring[]—
onExpandedChange(paths: string[]) => void—
searchable

Show the search field. Defaults to true.

booleantrue
querystring—
defaultQuerystring""
onQueryChange(query: string) => void—
pageSize

Children shown per page in a long array or object. Defaults to 50.

number50
copyable

Show copy value and copy path actions. Defaults to true.

booleantrue
onCopy(detail: JsonViewerCopyDetail) => void—
onSelect

Called when a row becomes the current one.

(detail: { path: string; value: unknown; type: JsonValueType }) => void—
showPath

Show the path of the current row under the tree. Defaults to true.

booleantrue
maxHeight

Height of the scrolling tree area. Defaults to 420px.

number | string420
label

Accessible name of the tree.

string"JSON"
classNamestring—

Also accepts the props of HTMLAttributes.

Dependencies

npm
lucide-reactmotion
Registry
silicon-motion-tokens