# JsonViewer
Package: @mantine/code-highlight
Import: import { JsonViewer } from '@mantine/code-highlight';
Description: Interactive JSON data viewer with expand/collapse, type indicators and copy to clipboard

## Usage

`JsonViewer` displays JSON data as a collapsible tree. Values are serialized the same way as with `JSON.stringify`,
for example, `Date` is displayed as a string and `Map` as an empty object:

```tsx
import { JsonViewer } from '@mantine/code-highlight';
import { data } from './data';

function Demo() {
  return <JsonViewer value={data} />;
}
```


## Default expand depth

Use `defaultExpandDepth` prop to set how many levels are expanded initially, the root node is always expanded:

```tsx
import { JsonViewer } from '@mantine/code-highlight';
import { data } from './data';

function Demo() {
  return <JsonViewer value={data} defaultExpandDepth={3} />;
}
```


`defaultExpandDepth` is applied only on mount. If `value` is loaded later, change the `key` together with the data,
or control the expanded state with `expandedPaths` and `onExpandedPathsChange` props:

```tsx
import { JsonViewer } from '@mantine/code-highlight';

function Demo({ data }: { data: unknown }) {
  return <JsonViewer key={JSON.stringify(data)} value={data} defaultExpandDepth={2} />;
}
```

## Long arrays and objects

Use `maxDisplayLength` prop to limit the number of entries displayed in a single object or array.
The remaining entries are replaced with a "... N more items" row:



Set `groupArraysAfterLength` prop to split longer arrays into collapsible groups instead.
`maxDisplayLength` does not apply to grouped arrays:



## Root name

Use `rootName` prop to display a label for the root node:

```tsx
import { JsonViewer } from '@mantine/code-highlight';
import { data } from './data';

function Demo() {
  return <JsonViewer value={data} rootName="response" />;
}
```


## Expand all / collapse all controls

Set `withControls` prop to display expand all and collapse all buttons. Controls are hidden when `allExpanded` is set:

```tsx
import { JsonViewer } from '@mantine/code-highlight';
import { data } from './data';

function Demo() {
  return (
    <JsonViewer value={data} withControls />
  );
}
```


## Type badges and size

Set `withTypes` prop to display type badges next to values and `withSize` prop
to display the number of items in arrays and objects:

```tsx
import { JsonViewer } from '@mantine/code-highlight';
import { data } from './data';

function Demo() {
  return <JsonViewer value={data} withTypes withSize />;
}
```


## Copy button

Set `withCopy` prop to display a copy button when a row is hovered or focused:

```tsx
import { JsonViewer } from '@mantine/code-highlight';
import { data } from './data';

function Demo() {
  return <JsonViewer value={data} withCopy />;
}
```


## Collapse long strings

Use `collapseStringsAfterLength` prop to truncate long strings behind a "show more" button.
Long strings wrap onto multiple lines, target the `data-wrap` attribute with [Styles API](https://alpha.mantine.dev/llms/styles-styles-api.md) to change that:

```tsx
import { JsonViewer } from '@mantine/code-highlight';
import { data } from './data';

function Demo() {
  return <JsonViewer value={data} collapseStringsAfterLength={30} />;
}
```


## Sort keys

Set `sortKeys` prop to sort object keys alphabetically or pass a comparator function:

```tsx
import { JsonViewer } from '@mantine/code-highlight';
import { data } from './data';

function Demo() {
  return <JsonViewer value={data} sortKeys />;
}
```


## Highlight items

Use `highlightItems` prop to highlight properties with diff colors. Keys are paths created with `serializeJsonViewerPath`, values are `added` or `removed`:

```tsx
import {
  JsonViewer,
  JsonViewerHighlightType,
  serializeJsonViewerPath,
} from '@mantine/code-highlight';
import { data } from './data';

const highlightItems: Record<string, JsonViewerHighlightType> = {
  [serializeJsonViewerPath(['version'])]: 'added',
  [serializeJsonViewerPath(['deprecated'])]: 'removed',
  [serializeJsonViewerPath(['author', 'url'])]: 'added',
};

function Demo() {
  return (
    <JsonViewer
      value={data}
      highlightItems={highlightItems}
      defaultExpandDepth={3}
    />
  );
}
```


## All expanded

Set `allExpanded` prop to expand all nodes and disable collapse interactions:

```tsx
import { JsonViewer } from '@mantine/code-highlight';
import { data } from './data';

function Demo() {
  return <JsonViewer value={data} allExpanded />;
}
```


## Line numbers

Set `withLineNumbers` prop to display line numbers alongside the JSON content:

```tsx
import { JsonViewer } from '@mantine/code-highlight';
import { data } from './data';

function Demo() {
  return <JsonViewer value={data} withLineNumbers />;
}
```


## Chevrons

Set `withChevrons` prop to display expand/collapse chevrons next to collapsible nodes:

```tsx
import { JsonViewer } from '@mantine/code-highlight';
import { data } from './data';

function Demo() {
  return <JsonViewer value={data} withChevrons />;
}
```


## Keyboard navigation

`JsonViewer` supports keyboard navigation similar to the [Tree](https://alpha.mantine.dev/llms/core-tree.md) component:

* `↑` / `↓` – move focus between visible nodes
* `→` – expand a collapsed node, or move focus to its first child
* `←` – collapse an expanded node, or move focus to its parent
* `Enter` / `Space` – toggle expand/collapse on collapsible nodes, trigger `onValueSelect` on primitive nodes
* `Ctrl + C` / `⌘ + C` – copy the focused node value when `withCopy` prop is set

## Accessibility

The content is rendered as an ARIA tree labelled with `rootName`, or `JSON` when it is not set.
The component is always rendered left-to-right, regardless of the application [direction](https://alpha.mantine.dev/llms/styles-rtl.md).


#### Props

**JsonViewer props**

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| allExpanded | boolean | false | When true, all nodes are expanded and expand/collapse is disabled |
| collapseAllLabel | string | 'Collapse all' | Label for collapse all button |
| collapseStringsAfterLength | number \| false | false | Collapse strings longer than this value |
| copiedLabel | string | 'Copied' | Label for copied state |
| copyLabel | string | 'Copy' | Label for copy button |
| defaultExpandDepth | number | 1 | Number of levels to expand by default. Applied only to the `value` present on mount; pass a `key` or use controlled `expandedPaths` to re-apply it after `value` changes. |
| expandAllLabel | string | 'Expand all' | Label for expand all button |
| expandedPaths | string[] | - | Controlled expanded paths |
| fontSize | MantineSize \| number | '13px' | Font size |
| groupArraysAfterLength | number \| false | false | Group arrays into chunks when array length exceeds this value |
| highlightItems | Record<string, JsonViewerHighlightType> | - | Map of JSON paths to highlight type ('added' or 'removed'), use serializeJsonViewerPath to build keys |
| indentWidth | number | 16 | Indent width in px |
| maxDisplayLength | number | 100 | Max number of items to show before truncation |
| onExpandedPathsChange | (paths: string[]) => void | - | Called when expanded paths change |
| onValueSelect | (path: string[], value: any) => void | - | Called when a value is selected/clicked |
| radius | MantineRadius \| number | 'sm' | Key of `theme.radius` or any valid CSS value to set border-radius |
| rootName | string \| false | false | Label displayed for the root node |
| sortKeys | boolean \| ((a: string, b: string) => number) | false | Sort object keys |
| value | any | required | JSON data to display |
| withBorder | boolean | false | Adds border to the root element |
| withChevrons | boolean | false | Whether to show expand/collapse chevrons |
| withControls | boolean | false | Whether to show expand all / collapse all controls |
| withCopy | boolean | false | Whether to show copy button on hover for individual nodes |
| withCopyButton | boolean | false | Whether to show a copy button to copy the entire JSON |
| withKeyQuotes | boolean | false | Whether to show quotes around keys |
| withLineNumbers | boolean | false | Whether to show line numbers |
| withQuotes | boolean | true | Whether to show quotes around string values |
| withSize | boolean | false | Whether to show item count for objects/arrays |
| withTypes | boolean | false | Whether to show type badges next to values |


#### Styles API

JsonViewer component supports Styles API. With Styles API, you can customize styles of any inner element. Follow the documentation to learn how to use CSS modules, CSS variables and inline styles to get full control over component styles.

**JsonViewer selectors**

| Selector | Static selector | Description |
|----------|----------------|-------------|
| root | .mantine-JsonViewer-root | Root element |
| node | .mantine-JsonViewer-node | Container for a single tree node |
| toggle | .mantine-JsonViewer-toggle | Expand/collapse chevron icon |
| key | .mantine-JsonViewer-key | Object key label |
| value | .mantine-JsonViewer-value | Primitive value display |
| bracket | .mantine-JsonViewer-bracket | Opening/closing brackets |
| type | .mantine-JsonViewer-type | Type badge |
| size | .mantine-JsonViewer-size | Item/key count label |
| ellipsis | .mantine-JsonViewer-ellipsis | Collapsed content indicator or show more/less button |
| copyButton | .mantine-JsonViewer-copyButton | Copy to clipboard button |
| content | .mantine-JsonViewer-content | Content wrapper with padding |
| row | .mantine-JsonViewer-row | Single row wrapper for a node |
| scrollarea | .mantine-JsonViewer-scrollarea | Scroll area wrapper |
| lineNumbers | .mantine-JsonViewer-lineNumbers | Line numbers column |
| wrapper | .mantine-JsonViewer-wrapper | Flex wrapper around line numbers and content |
| copyAllButton | .mantine-JsonViewer-copyAllButton | Copy all JSON button positioned at top-right corner |
| controls | .mantine-JsonViewer-controls | Expand all / collapse all controls container |
| control | .mantine-JsonViewer-control | Expand all / collapse all button |

**JsonViewer CSS variables**

| Selector | Variable | Description |
|----------|----------|-------------|
| root | --jv-radius | Border radius |
| root | --jv-fz | Font size |
| root | --jv-indent | Indent width per nesting level |

**JsonViewer data attributes**

| Selector | Attribute | Condition | Value |
|----------|-----------|-----------|-------|
| root | data-with-border | `withBorder` prop is set | - |
| node | data-type | - | `array`, `object` or primitive value type name |
| node | data-highlight | Node path is present in `highlightItems` | `added` or `removed` |
| row | data-root | Root node row | - |
| row | data-hoverable | Row can be expanded, collapsed or selected | - |
| toggle | data-expanded | Node is expanded | - |
