---
title: "JSON Viewer"
description: "Shows a JSON value as a tree that people open and close by key. They can copy a value, see its type, and see what changed."
url: https://gelatinui.com/components/json-viewer
markdown_url: https://gelatinui.com/components/json-viewer.md
access: pro
install: "npx shadcn@latest add @gelatin-ui-pro/json-viewer"
version: "2026-11-09"
index: https://gelatinui.com/llms.txt
docs_index: https://gelatinui.com/docs/llms.txt
---

# JSON Viewer

> Shows a JSON value as a tree that people open and close by key. They can copy a value, see its type, and see what changed.

Page: https://gelatinui.com/components/json-viewer

## When to use it

Best for: Read a JSON value by key: open the parts you need, copy a value, and see what changed.

- Use it to show JSON data that people read and copy, such as an API response or a log entry.
- Use `marks` to show inserted, deleted, and changed keys.
- Use `JsonViewerContent virtualized` with a fixed height for thousands of rows.
- Put it in a `CodeBlock` with `JsonViewerExpandAll` and `JsonViewerCollapseAll` in the header.

## When not to use it

- To edit the JSON text, use a [Code Editor](https://gelatinui.com/components/code-editor.md).
- To show JSON as plain text with highlight, use a [Code Block](https://gelatinui.com/components/code-block.md).
- For a short list of label and value pairs, use a [Data List](https://gelatinui.com/components/data-list.md).

## Notes for agents

- `value` takes any JS value. A Map, Set, Date, or bigint shows as JSON. A circular reference shows as `[Circular]`.
- Paths are ids such as `$.user.roles`. Use them in `defaultExpandedPaths`, `expandedPaths`, and `marks`.
- `defaultExpandDepth` is 1 by default: the root is open and its keys are closed. `Infinity` opens all.
- Long objects show 100 keys and a Show more row (`maxItems`). Long arrays split into ranges (`groupArraysAfter`).
- `onValueSelect` runs on Enter or a press on a row that cannot open.
- With no children it renders `<JsonViewerContent />`.

## Anatomy

```text
JsonViewer
├─ JsonViewerContent (0..n)
├─ JsonViewerExpandAll (0..n)
└─ JsonViewerCollapseAll (0..n)
```

## API reference

### JsonViewer

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `palette` | `"color" \| "ink" \| "theme"` |  |  |
| `value` | `unknown` |  | The value to show: JSON data, or any JS value (a Map, Set, Date, or bigint reads as JSON). |
| `rootName` | `string` |  | Show the value itself as the first row with this name; it also names the tree. |
| `defaultExpandDepth` | `number` | 1 | How many levels start open, on the first render. Default 1: the root is open, its keys are closed. `Infinity` opens all. The top keys are level 1 with or without `rootName`. |
| `expandedPaths` | `Iterable<string>` |  | The open rows (controlled), by path id (`formatJsonPath`: `$.user.roles`). |
| `defaultExpandedPaths` | `Iterable<string>` |  | The open rows at first (uncontrolled). Wins over `defaultExpandDepth`. |
| `onExpandedPathsChange` | `function` |  |  |
| `sortKeys` | `JsonSortKeys` |  | Sort object keys: `true` sorts a to z; a function sorts as `Array.prototype.sort`. |
| `maxItems` | `number` | 100 | The keys or items a row shows before a "Show more" row. Default 100. |
| `groupArraysAfter` | `number` | 100 | Split an array longer than this into closed ranges of this many items. Default 100; 0 off. |
| `maxStringLength` | `number` |  | Cut a string longer than this, with a button that shows the rest. Default: no cut. |
| `types` | `boolean` | false | Show the type of each value at the row end (`string`, `number`, …). Default `false`. |
| `counts` | `boolean` | true | Show the number of keys or items: `{3}` and `[2]`, else `{…}`. Default `true`. |
| `copy` | `boolean` | true | A copy button on each row (and Cmd/Ctrl+C on the focused row). Default `true`. |
| `lineNumbers` | `boolean` | false | The row number at the start of each shown row. Default `false`. |
| `marks` | `Readonly<Record<string, JsonViewerMark>>` |  | Change marks by path id: a tint, and a + or − sign for `inserted` and `deleted`. |
| `wrap` | `boolean` |  | Wrap long strings over lines. Default: the Wrap toggle of the Code Block, else `false`. |
| `onValueSelect` | `function` |  | Runs on Enter or a press on a row that cannot open (a value, an empty object or array), with its path and its value as JSON data. Rows that open keep opening on a press. |
| `labels` | `Partial<JsonViewerLabels>` |  | The words of the viewer (`JsonViewerLabels` in `#/lib/pro/json-tree-model`). |
| `children` | `ReactNode` |  | The parts. Default: `<JsonViewerContent />`. |

### JsonViewerContent

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `aria-label` | `string` |  | Defines a string value that labels the current element. |
| `aria-labelledby` | `string` |  | Identifies the element (or elements) that labels the current element. |
| `aria-describedby` | `string` |  | Identifies the element (or elements) that describes the object. |
| `aria-details` | `string` |  | Identifies the element (or elements) that provide a detailed, extended description for the object. |
| `id` | `string` |  | The element's unique identifier. See MDN. |
| `lang` | `string` |  |  |
| `hidden` | `boolean` |  |  |
| `inert` | `boolean` |  |  |
| `translate` | `"no" \| "yes"` |  |  |
| `suppressHydrationWarning` | `boolean` |  | A React-only flag that suppresses the warning React normally logs when an element's server-rendered content doesn't match the client render during hydration. See React docs. |
| `onClick` | `MouseEventHandler<HTMLDivElement>` |  |  |
| `onClickCapture` | `MouseEventHandler<HTMLDivElement>` |  |  |
| `onAuxClick` | `MouseEventHandler<HTMLDivElement>` |  |  |
| `onAuxClickCapture` | `MouseEventHandler<HTMLDivElement>` |  |  |
| `onContextMenu` | `MouseEventHandler<HTMLDivElement>` |  |  |
| `onContextMenuCapture` | `MouseEventHandler<HTMLDivElement>` |  |  |
| `onDoubleClick` | `MouseEventHandler<HTMLDivElement>` |  |  |
| `onDoubleClickCapture` | `MouseEventHandler<HTMLDivElement>` |  |  |
| `onMouseDown` | `MouseEventHandler<HTMLDivElement>` |  |  |
| `onMouseDownCapture` | `MouseEventHandler<HTMLDivElement>` |  |  |
| `onMouseEnter` | `MouseEventHandler<HTMLDivElement>` |  |  |
| `onMouseLeave` | `MouseEventHandler<HTMLDivElement>` |  |  |
| `onMouseMove` | `MouseEventHandler<HTMLDivElement>` |  |  |
| `onMouseMoveCapture` | `MouseEventHandler<HTMLDivElement>` |  |  |
| `onMouseOut` | `MouseEventHandler<HTMLDivElement>` |  |  |
| `onMouseOutCapture` | `MouseEventHandler<HTMLDivElement>` |  |  |
| `onMouseOver` | `MouseEventHandler<HTMLDivElement>` |  |  |
| `onMouseOverCapture` | `MouseEventHandler<HTMLDivElement>` |  |  |
| `onMouseUp` | `MouseEventHandler<HTMLDivElement>` |  |  |
| `onMouseUpCapture` | `MouseEventHandler<HTMLDivElement>` |  |  |
| `onTouchCancel` | `TouchEventHandler<HTMLDivElement>` |  |  |
| `onTouchCancelCapture` | `TouchEventHandler<HTMLDivElement>` |  |  |
| `onTouchEnd` | `TouchEventHandler<HTMLDivElement>` |  |  |
| `onTouchEndCapture` | `TouchEventHandler<HTMLDivElement>` |  |  |
| `onTouchMove` | `TouchEventHandler<HTMLDivElement>` |  |  |
| `onTouchMoveCapture` | `TouchEventHandler<HTMLDivElement>` |  |  |
| `onTouchStart` | `TouchEventHandler<HTMLDivElement>` |  |  |
| `onTouchStartCapture` | `TouchEventHandler<HTMLDivElement>` |  |  |
| `onPointerDown` | `PointerEventHandler<HTMLDivElement>` |  |  |
| `onPointerDownCapture` | `PointerEventHandler<HTMLDivElement>` |  |  |
| `onPointerMove` | `PointerEventHandler<HTMLDivElement>` |  |  |
| `onPointerMoveCapture` | `PointerEventHandler<HTMLDivElement>` |  |  |
| `onPointerUp` | `PointerEventHandler<HTMLDivElement>` |  |  |
| `onPointerUpCapture` | `PointerEventHandler<HTMLDivElement>` |  |  |
| `onPointerCancel` | `PointerEventHandler<HTMLDivElement>` |  |  |
| `onPointerCancelCapture` | `PointerEventHandler<HTMLDivElement>` |  |  |
| `onPointerEnter` | `PointerEventHandler<HTMLDivElement>` |  |  |
| `onPointerLeave` | `PointerEventHandler<HTMLDivElement>` |  |  |
| `onPointerOver` | `PointerEventHandler<HTMLDivElement>` |  |  |
| `onPointerOverCapture` | `PointerEventHandler<HTMLDivElement>` |  |  |
| `onPointerOut` | `PointerEventHandler<HTMLDivElement>` |  |  |
| `onPointerOutCapture` | `PointerEventHandler<HTMLDivElement>` |  |  |
| `onGotPointerCapture` | `PointerEventHandler<HTMLDivElement>` |  |  |
| `onGotPointerCaptureCapture` | `PointerEventHandler<HTMLDivElement>` |  |  |
| `onLostPointerCapture` | `PointerEventHandler<HTMLDivElement>` |  |  |
| `onLostPointerCaptureCapture` | `PointerEventHandler<HTMLDivElement>` |  |  |
| `onScroll` | `UIEventHandler<HTMLDivElement>` |  |  |
| `onScrollCapture` | `UIEventHandler<HTMLDivElement>` |  |  |
| `onWheel` | `WheelEventHandler<HTMLDivElement>` |  |  |
| `onWheelCapture` | `WheelEventHandler<HTMLDivElement>` |  |  |
| `onAnimationStart` | `AnimationEventHandler<HTMLDivElement>` |  |  |
| `onAnimationStartCapture` | `AnimationEventHandler<HTMLDivElement>` |  |  |
| `onAnimationEnd` | `AnimationEventHandler<HTMLDivElement>` |  |  |
| `onAnimationEndCapture` | `AnimationEventHandler<HTMLDivElement>` |  |  |
| `onAnimationIteration` | `AnimationEventHandler<HTMLDivElement>` |  |  |
| `onAnimationIterationCapture` | `AnimationEventHandler<HTMLDivElement>` |  |  |
| `onTransitionCancel` | `TransitionEventHandler<HTMLDivElement>` |  |  |
| `onTransitionCancelCapture` | `TransitionEventHandler<HTMLDivElement>` |  |  |
| `onTransitionEnd` | `TransitionEventHandler<HTMLDivElement>` |  |  |
| `onTransitionEndCapture` | `TransitionEventHandler<HTMLDivElement>` |  |  |
| `onTransitionRun` | `TransitionEventHandler<HTMLDivElement>` |  |  |
| `onTransitionRunCapture` | `TransitionEventHandler<HTMLDivElement>` |  |  |
| `onTransitionStart` | `TransitionEventHandler<HTMLDivElement>` |  |  |
| `onTransitionStartCapture` | `TransitionEventHandler<HTMLDivElement>` |  |  |
| `selectionMode` | `SelectionMode` |  | The type of selection that is allowed in the collection. |
| `disallowEmptySelection` | `boolean` |  | Whether the collection allows empty selection. |
| `disabledKeys` | `Iterable<Key>` |  | The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with. |
| `slot` | `string \| null` |  | A slot name for the component. Slots allow the component to receive props from a parent component. An explicit `null` value indicates that the local props completely override all props received from a parent. |
| `render` | `DOMRenderFunction` |  | Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components. Requirements: - You must render the expected element type (e.g. if `<button>` is expected, you cannot render an `<a>`). - Only a single root DOM element can be rendered (no fragments). - You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate. |
| `value` | `"all" \| Iterable<Key>` |  | The picked keys (controlled). |
| `defaultValue` | `"all" \| Iterable<Key>` |  | The picked keys at first (uncontrolled). |
| `onValueChange` | `function` |  | Runs on each change, with the new picked keys and the Base UI change details. |
| `dependencies` | `readonly any[]` |  | Values that should invalidate the item cache when using dynamic collections. |
| `autoFocus` | `boolean \| FocusStrategy` |  | Whether to auto focus the gridlist or an option. |
| `onAction` | `function` |  | Handler that is called when a user performs an action on an item. The exact user event depends on the collection's `selectionBehavior` prop and the interaction modality. |
| `shouldSelectOnPressUp` | `boolean` |  | Whether selection should occur on press up instead of press down. |
| `keyboardNavigationBehavior` | `"arrow" \| "tab"` | 'arrow' | Whether keyboard navigation to focusable elements within grid list items is via the left/right arrow keys or the tab key. |
| `escapeKeyBehavior` | `"clearSelection" \| "none"` | 'clearSelection' | Whether pressing the escape key should clear selection in the grid list or not. Most experiences should not modify this option as it eliminates a keyboard user's ability to easily clear selection. Only use if the escape key is being handled externally or should not trigger selection clearing contextually. |
| `selectionBehavior` | `SelectionBehavior` | 'toggle' | How multiple selection should behave in the tree. |
| `renderEmptyState` | `function` |  | Provides content to display when there are no items in the list. |
| `disabledBehavior` | `DisabledBehavior` | 'all' | Whether `disabledKeys` applies to all interactions, or only selection. |
| `dragAndDropHooks` | `DragAndDropHooks<NoInfer<JsonTreeRow>>` |  | The drag and drop hooks returned by `useDragAndDrop` used to enable drag and drop behavior for the Tree. |
| `className` | `string` |  |  |
| `ref` | `Ref<HTMLDivElement>` |  |  |
| `flat` | `boolean` |  | A list with no parent rows: no row keeps the 20px chevron lane (and its 6px gap) empty, so the icon of each row sits at the row padding. For a flat list of the Tree look (the list view of Changed Files). A row with child rows still shows its chevron. Default `false`. |
| `selectionCascade` | `boolean` |  | Checks follow the folders (`selectionMode="multiple"`): checking a row checks every row under it, also in closed folders and at every depth; unchecking clears them; a parent is checked when all its rows are, and shows the dash when some are. Disabled rows are skipped, and a disabled folder shows the state of its rows. Rows that load or drop into a folder the user checked arrive checked, unless the user unchecked that exact row (`#/lib/tree-selection-model`, `TreeCheckIntent`). `onValueChange` gets the whole result, so a controlled `value` works. It runs on a user change, and when rows load into a checked folder; never on the mount or a render of the parent (there the Tree only draws the whole selection). Default `false`: React Aria's independent selection, where a folder is an item of its own. |
| `virtualized` | `boolean` | false | Render only the rows in view (React Aria `Virtualizer`, fixed 24px rows, 44px on touch). Give the tree a height (`className="h-96"`). Strings do not wrap while it is on. Default `false`: turn it on for values with thousands of shown rows. |

### JsonViewerExpandAll

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `className` | `string` |  |  |
| `busy` | `boolean` |  | The button waits for the app (busy words): a spinner, `aria-busy`, and no clicks. Focus stays on the button. The style hook stays `data-loading`. |
| `spinnerPlacement` | `"center" \| "start"` |  | `center` (default): the spinner covers the label. `start`: it takes the leading icon slot. The icon sizes always use `center`, because their one icon is the label. |
| `glass` | `"clear" \| "dark" \| "light"` |  |  |
| `rounded` | `"default" \| "full"` |  |  |
| `size` | `"default" \| "icon" \| "icon-lg" \| "icon-sm" \| "icon-xs" \| "lg" \| "sm" \| "xl" \| "xs"` | "icon-xs" |  |
| `variant` | `"dashed" \| "default" \| "destructive" \| "destructive-outline" \| "ghost" \| "link" \| "media" \| "outline" \| "secondary"` | "ghost" |  |
| `children` | `ReactNode` |  |  |

Also takes every prop of Base UI `Button`: https://base-ui.com/react/components/button

### JsonViewerCollapseAll

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `className` | `string` |  |  |
| `busy` | `boolean` |  | The button waits for the app (busy words): a spinner, `aria-busy`, and no clicks. Focus stays on the button. The style hook stays `data-loading`. |
| `spinnerPlacement` | `"center" \| "start"` |  | `center` (default): the spinner covers the label. `start`: it takes the leading icon slot. The icon sizes always use `center`, because their one icon is the label. |
| `glass` | `"clear" \| "dark" \| "light"` |  |  |
| `rounded` | `"default" \| "full"` |  |  |
| `size` | `"default" \| "icon" \| "icon-lg" \| "icon-sm" \| "icon-xs" \| "lg" \| "sm" \| "xl" \| "xs"` | "icon-xs" |  |
| `variant` | `"dashed" \| "default" \| "destructive" \| "destructive-outline" \| "ghost" \| "link" \| "media" \| "outline" \| "secondary"` | "ghost" |  |
| `children` | `ReactNode` |  |  |

Also takes every prop of Base UI `Button`: https://base-ui.com/react/components/button

## Styling

| Part | Slot | States | CSS variables | Tokens |
| --- | --- | --- | --- | --- |
| JsonViewer | `json-viewer-content` |  |  |  |
| JsonViewerContent | `json-viewer-content` |  |  |  |
| JsonViewerExpandAll | `json-viewer-expand-all` |  |  |  |
| JsonViewerCollapseAll | `json-viewer-collapse-all` |  |  |  |

## Usage

Put the viewer in a `CodeBlock`. Compose the header buttons and the `JsonViewerContent` tree. A `rootName` names the tree.

The sample also uses `code-block`, which this add does not write. One command adds them with this item:

```bash
npx shadcn@latest add @gelatin-ui-pro/json-viewer @gelatin-ui/code-block
```

```tsx title="api-response.tsx"
import { CodeBlock, CodeBlockCopyButton, CodeBlockHeader, CodeBlockTitle } from "@/components/ui/code-block";
import {
  JsonViewer,
  JsonViewerCollapseAll,
  JsonViewerContent,
  JsonViewerExpandAll,
} from "@/components/pro/code/json-viewer";

const RESPONSE = {
  id: "cus_9s6XKzkNRiz8i3",
  email: "mira@okafor.dev",
  roles: ["admin", "billing"],
  verified: true,
};

export function ApiResponse() {
  return (
    <CodeBlock palette="color" className="w-xl max-w-full">
      <JsonViewer value={RESPONSE} rootName="customer" defaultExpandDepth={2}>
        <CodeBlockHeader>
          <CodeBlockTitle>200 · GET /v1/customers/cus_9s6X</CodeBlockTitle>
          <JsonViewerExpandAll />
          <JsonViewerCollapseAll />
          <CodeBlockCopyButton />
        </CodeBlockHeader>
        <JsonViewerContent aria-label="Response body" />
      </JsonViewer>
    </CodeBlock>
  );
}
```

## Compose

Rules:

- Compose the parts (`compose-parts`): Build each item from its named parts as children, and pass your text and icons in as parts.
- Every state is designed (`every-state`): Give each view of data its loading, empty, and error states: a skeleton in the final layout, an `Empty` with an illustration and one next action, and an error that says what failed and how to retry.

## Add it

```bash
npx shadcn@latest add @gelatin-ui-pro/json-viewer
```

It adds 61 registry items: 58 files, 9805 lines, and the npm packages `@base-ui/react`, `@mantine/hooks`, `@phosphor-icons/react`, `@tanstack/highlight`, `@xstate/react`, `clsx`, `motion`, `react-aria-components`, `tailwind-merge`, `tailwind-variants`, `xstate`.

## Built on

- [Button](https://gelatinui.com/components/button.md)
- [Code](https://gelatinui.com/components/code.md)
- [Copy Button](https://gelatinui.com/components/copy-button.md)
- [Tree](https://gelatinui.com/components/tree.md)

## For AI agents

- Every item, one file: https://gelatinui.com/llms.txt
- MCP server (`search_items`, `get_item`, `get_install_plan`): https://gelatinui.com/mcp

## Related

- [Code Block](https://gelatinui.com/components/code-block.md)
- [Tree](https://gelatinui.com/components/tree.md)
- [Code Editor](https://gelatinui.com/components/code-editor.md)
- [Copy Button](https://gelatinui.com/components/copy-button.md)

## Keyboard

| Keys | Action |
| --- | --- |
| Arrow Down, Arrow Up | Moves focus to the next or the previous row. |
| Arrow Right, Arrow Left | Opens or closes the object or array that has focus. |
| Home, End | Moves focus to the first or the last row. |
| Letter keys | Moves focus to the next row whose key starts with the typed letters. |
| Enter | Runs `onValueSelect` on a value row, or loads a Show more row. |
| ⌘C, Ctrl+C | Copies the value of the row that has focus, when no text is selected. |

## Accessibility

The rows are the Tree component: a React Aria `treegrid` with one tab stop, arrow keys, and type-ahead on the keys.

The tree is named by `rootName`, else "JSON", unless you give it `aria-label` or `aria-labelledby`.

An inserted or deleted mark shows a + or a − sign too, so a change does not rest on color only.

The copy button of each row has a name, and on touch each row is 44px high.

## Motion

| Part | Motion |
| --- | --- |
| Row hover | hover · 150ms · ease-out (0, 0, 0.2, 1) |
