Interactive Answers
JSON Viewer
Structured output as a collapsible tree — types, counts, search, copy path and copy value.
3 variantsMIT
Preview
Installation
Copies the component into components/ui and installs its dependencies and theme tokens.
pnpm dlx shadcn@latest add https://uilab.tmih06.com/r/json-viewer.jsonUsage
Pass your own data and wire the callbacks to your agent runtime.
import { JsonViewer } from "@/components/ui/json-viewer";
export function ToolResult({ call }: { call: { name: string; result: unknown } }) {
return (
<JsonViewer
title={call.name}
data={call.result as JsonValue}
defaultDepth={1}
onCopy={(kind, text) => toast(kind === "path" ? `Copied ${text}` : "Value copied")}
/>
);
}
// Pass null while the tool call is running; the tree lines load as blank,
// nested placeholders — never anything that reads as copyable JSON.
export function ToolResultState({ query }: { query: ToolQuery }) {
return (
<JsonViewer
title={query.name}
data={query.data ?? null}
loading={query.isRefetching}
skeleton={{ count: 9 }}
/>
);
}Props
| Prop | Type | Default | Description |
|---|---|---|---|
| data* | JsonValue | null | — | Any JSON-serializable value. Pass null while the result loads — a top-level JSON null now means "not loaded", so wrap it, e.g. `{ value: null }`. |
| loading | boolean | false | Force the layout-preserving skeleton while refetching, even when stale data is available. |
| skeleton | { count?: number } | — | Shape hints for the loading state; count is the number of tree line placeholders. |
| title | string | "Output" | Header title and root label. |
| view | "tree" | "raw" | "tree" | Collapsible tree with search, or pretty-printed JSON with a copy button. |
| defaultDepth | number | 1 | Levels open on first render. Search opens every ancestor of a match. |
| onCopy | (kind: "path" | "value", text: string) => void | — | Called after copying — clicking a key copies its path ($.items[2].price), the row button copies its value. |
| labels | Partial<JsonViewerLabels> | — | Your own UI strings (Expand all, Copy JSON, the keyboard hint, the loading announcement…), for translating. Right-to-left works under `dir="rtl"`; the JSON stays left to right. |
| className | string | — | Extra classes for the shell. |
type JsonValue = string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue };
// Keyboard: ↑ ↓ move, → opens or steps in, ← closes or steps out, Enter toggles or copies.