# JSON Tree View URL: https://ark-ui.com/docs/utilities/json-tree-view Source: https://raw.githubusercontent.com/chakra-ui/ark/refs/heads/main/website/src/content/pages/utilities/json-tree-view.mdx A component that displays JSON data in an interactive, collapsible tree structure. --- ## Anatomy To set up the JSON tree view correctly, you'll need to understand its anatomy and how we name its parts. > Each part includes a `data-part` attribute to help identify them in the DOM. ## Examples Learn how to use the `JsonTreeView` component in your project. Let's take a look at the most basic example: **Example: basic** #### React ```tsx import { JsonTreeView } from '@ark-ui/react/json-tree-view' import { ChevronRightIcon } from 'lucide-react' export const Basic = () => { return ( } /> ) } ``` #### Solid ```tsx import { JsonTreeView } from '@ark-ui/solid/json-tree-view' import { ChevronRightIcon } from 'lucide-solid' export const Basic = () => { return ( } /> ) } ``` #### Vue ```vue ``` #### Svelte ```svelte {#snippet arrow()} {/snippet} ``` ### Different Data Types The JSON tree view can display various JavaScript data types including objects, arrays, primitives, and special values: **Example: array-data** #### React ```tsx import { JsonTreeView } from '@ark-ui/react/json-tree-view' import { ChevronRightIcon } from 'lucide-react' const testArray = [1, 2, 3, 4, 5] Object.defineProperties(testArray, { customProperty: { value: 'custom value', enumerable: false, writable: false }, anotherProperty: { value: 42, enumerable: false, writable: false }, }) export const ArrayData = () => { return ( { const sparse = [] sparse[0] = 'first' sparse[5] = 'sixth' return sparse })(), }} > } /> ) } ``` #### Solid ```tsx import { JsonTreeView } from '@ark-ui/solid/json-tree-view' import { ChevronRightIcon } from 'lucide-solid' const testArray = [1, 2, 3, 4, 5] Object.defineProperties(testArray, { customProperty: { value: 'custom value', enumerable: false, writable: false }, anotherProperty: { value: 42, enumerable: false, writable: false }, }) export const ArrayData = () => { return ( { const sparse = [] sparse[0] = 'first' sparse[5] = 'sixth' return sparse })(), }} > } /> ) } ``` #### Vue ```vue ``` #### Svelte ```svelte { const sparse = [] sparse[0] = 'first' sparse[5] = 'sixth' return sparse })(), }} > {#snippet arrow()} {/snippet} ``` ### Functions and Methods Display JavaScript functions, async functions, and generators in your JSON tree: **Example: functions** #### React ```tsx import { JsonTreeView } from '@ark-ui/react/json-tree-view' import { ChevronRightIcon } from 'lucide-react' const data = [ function sum(a: number, b: number) { return a + b }, async (promises: Promise[]) => await Promise.all(promises), function* generator(a: number) { while (a > 0) { yield a - 1 } }, ] export const Functions = () => { return ( } /> ) } ``` #### Solid ```tsx import { JsonTreeView } from '@ark-ui/solid/json-tree-view' import { ChevronRightIcon } from 'lucide-solid' const data = [ function sum(a: number, b: number) { return a + b }, async (promises: Promise[]) => await Promise.all(promises), function* generator(a: number) { while (a > 0) { yield a - 1 } }, ] export const Functions = () => { return ( } /> ) } ``` #### Vue ```vue ``` #### Svelte ```svelte {#snippet arrow()} {/snippet} ``` ### Regular Expressions Regular expressions are displayed with their pattern and flags: **Example: regex** #### React ```tsx import { JsonTreeView } from '@ark-ui/react/json-tree-view' import { ChevronRightIcon } from 'lucide-react' const data = { regex: /^[a-z0-9]+/g, case_insensitive: /^(?:[a-z0-9]+)foo.*?/i, } export const Regex = () => { return ( } /> ) } ``` #### Solid ```tsx import { JsonTreeView } from '@ark-ui/solid/json-tree-view' import { ChevronRightIcon } from 'lucide-solid' const data = { regex: /^[a-z0-9]+/g, case_insensitive: /^(?:[a-z0-9]+)foo.*?/i, } export const Regex = () => { return ( } /> ) } ``` #### Vue ```vue ``` #### Svelte ```svelte {#snippet arrow()} {/snippet} ``` ### Error Objects Error objects and their stack traces can be visualized: **Example: errors** #### React ```tsx import { JsonTreeView } from '@ark-ui/react/json-tree-view' import { ChevronRightIcon } from 'lucide-react' const data = new Error('Error') export const Errors = () => { return ( } /> ) } ``` #### Solid ```tsx import { JsonTreeView } from '@ark-ui/solid/json-tree-view' import { ChevronRightIcon } from 'lucide-solid' const data = new Error('Error') export const Errors = () => { return ( } /> ) } ``` #### Vue ```vue ``` #### Svelte ```svelte {#snippet arrow()} {/snippet} ``` ### Map and Set Objects Native JavaScript Map and Set objects are supported: **Example: map-and-set** #### React ```tsx import { JsonTreeView } from '@ark-ui/react/json-tree-view' import { ChevronRightIcon } from 'lucide-react' const data = new Map([ ['name', 'ark-ui-json-tree'], ['license', 'MIT'], ['elements', new Set(['ark-ui', 123, false, true, null, undefined, 456n])], [ 'nested', new Map([ [ 'taglines', new Set([ { name: 'ark-ui', feature: 'headless components' }, { name: 'ark-ui', feature: 'framework agnostic' }, { name: 'ark-ui', feature: 'accessible by default' }, ]), ], ]), ], ]) export const MapAndSet = () => { return ( } /> ) } ``` #### Solid ```tsx import { JsonTreeView } from '@ark-ui/solid/json-tree-view' import { ChevronRightIcon } from 'lucide-solid' const data = new Map([ ['name', 'ark-ui-json-tree'], ['license', 'MIT'], ['elements', new Set(['ark-ui', 123, false, true, null, undefined, 456n])], [ 'nested', new Map([ [ 'taglines', new Set([ { name: 'ark-ui', feature: 'headless components' }, { name: 'ark-ui', feature: 'framework agnostic' }, { name: 'ark-ui', feature: 'accessible by default' }, ]), ], ]), ], ]) export const MapAndSet = () => { return ( } /> ) } ``` #### Vue ```vue ``` #### Svelte ```svelte {#snippet arrow()} {/snippet} ``` ### Controlling Expand Level Use the `defaultExpandedDepth` prop to control how many levels are expanded by default: **Example: expand-level** #### React ```tsx import { JsonTreeView } from '@ark-ui/react/json-tree-view' import { ChevronRightIcon } from 'lucide-react' export const ExpandLevel = () => { return ( } /> ) } ``` #### Solid ```tsx import { JsonTreeView } from '@ark-ui/solid/json-tree-view' import { ChevronRightIcon } from 'lucide-solid' export const ExpandLevel = () => { return ( } /> ) } ``` #### Vue ```vue ``` #### Svelte ```svelte {#snippet arrow()} {/snippet} ``` ### Custom Value Rendering You can customize how specific values are rendered using the `renderValue` prop. This example shows how to make email addresses clickable: **Example: render-value** #### React ```tsx import { JsonTreeView } from '@ark-ui/react/json-tree-view' import { ChevronRightIcon } from 'lucide-react' export const RenderValue = () => { return ( } renderValue={(node) => { if (node.type === 'text' && typeof node.value === 'string' && isEmail(node.value)) { return ( {node.value} ) } }} /> ) } const isEmail = (value: string) => { const strippedValue = value.replace(/^"(.*)"$/, '$1') return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(strippedValue) } ``` #### Solid ```tsx import { JsonTreeView } from '@ark-ui/solid/json-tree-view' import { ChevronRightIcon } from 'lucide-solid' export const RenderValue = () => { return ( } renderValue={(node) => { if (node.type === 'text' && typeof node.value === 'string' && isEmail(node.value)) { return ( {node.value} ) } }} /> ) } const isEmail = (value: string) => { const strippedValue = value.replace(/^"(.*)"$/, '$1') return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(strippedValue) } ``` #### Vue ```vue ``` #### Svelte ```svelte {#snippet arrow()} {/snippet} {#snippet renderValue(node)} {#if node.type === 'text' && typeof node.value === 'string'} {#if isEmail(node.value)} {node.value} {:else} {node.value} {/if} {/if} {/snippet} ``` ### Configuration Options The JSON tree view supports several configuration options to customize the display: ```tsx } /> ``` **Configuration Options:** - **`quotesOnKeys`**: Whether to show quotes around object keys - **`showNonenumerable`**: Whether to show non-enumerable properties - **`maxPreviewItems`**: Maximum number of items to show in object/array previews - **`collapseStringsAfterLength`**: Collapse strings longer than this length - **`groupArraysAfterLength`**: Group array items when array is longer than this length ### Using the Root Provider The `RootProvider` component provides a context for the JSON tree view. It accepts the value of the `useJsonTreeView` hook. You can leverage it to access the component state and methods from outside the JSON tree view. **Example: root-provider** #### React ```tsx import { JsonTreeView, useJsonTreeView } from '@ark-ui/react/json-tree-view' import { ChevronRightIcon } from 'lucide-react' export const RootProvider = () => { const jsonTreeView = useJsonTreeView({ data: { name: 'John Doe', age: 30, email: 'john.doe@example.com', tags: ['tag1', 'tag2', 'tag3'], address: { street: '123 Main St', city: 'Anytown', state: 'CA', zip: '12345', }, }, }) return ( } /> ) } ``` #### Solid ```tsx import { JsonTreeView, useJsonTreeView } from '@ark-ui/solid/json-tree-view' import { ChevronRightIcon } from 'lucide-solid' export const RootProvider = () => { const jsonTreeView = useJsonTreeView({ data: { name: 'John Doe', age: 30, email: 'john.doe@example.com', tags: ['tag1', 'tag2', 'tag3'], address: { street: '123 Main St', city: 'Anytown', state: 'CA', zip: '12345', }, }, }) return ( } /> ) } ``` #### Vue ```vue ``` #### Svelte ```svelte {#snippet arrow()} {/snippet} ``` > If you're using the `RootProvider` component, you don't need to use the `Root` component. ## API Reference **Component API Reference** #### React **JsonTreeViewRoot Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `checkedValue` | `string[]` | No | The controlled checked node value | | `collapseStringsAfterLength` | `number` | No | | | `data` | `{}` | No | The data to display in the tree. | | `defaultCheckedValue` | `string[]` | No | The initial checked node value when rendered. Use when you don't need to control the checked node value. | | `defaultExpandedDepth` | `number` | No | The default expand level. | | `defaultExpandedValue` | `string[]` | No | The initial expanded node ids when rendered. Use when you don't need to control the expanded node value. | | `defaultFocusedValue` | `string` | No | The initial focused node value when rendered. Use when you don't need to control the focused node value. | | `defaultSelectedValue` | `string[]` | No | The initial selected node value when rendered. Use when you don't need to control the selected node value. | | `expandedValue` | `string[]` | No | The controlled expanded node ids | | `expandOnClick` | `boolean` | No | Whether clicking on a branch should open it or not | | `focusedValue` | `string` | No | The value of the focused node | | `groupArraysAfterLength` | `number` | No | | | `ids` | `Partial<{ root: string; tree: string; label: string; node: (value: string) => string }>` | No | The ids of the tree elements. Useful for composition. | | `lazyMount` | `boolean` | No | Whether to enable lazy mounting | | `loadChildren` | `(details: LoadChildrenDetails>) => Promise[]>` | No | Function to load children for a node asynchronously. When provided, branches will wait for this promise to resolve before expanding. | | `maxPreviewItems` | `number` | No | | | `onCheckedChange` | `(details: CheckedChangeDetails) => void` | No | Called when the checked value changes | | `onExpandedChange` | `(details: ExpandedChangeDetails>) => void` | No | Called when the tree is opened or closed | | `onFocusChange` | `(details: FocusChangeDetails>) => void` | No | Called when the focused node changes | | `onLoadChildrenComplete` | `(details: LoadChildrenCompleteDetails>) => void` | No | Called when a node finishes loading children | | `onLoadChildrenError` | `(details: LoadChildrenErrorDetails>) => void` | No | Called when loading children fails for one or more nodes | | `onSelectionChange` | `(details: SelectionChangeDetails>) => void` | No | Called when the selection changes | | `quotesOnKeys` | `boolean` | No | Whether to show quotes on the keys. | | `selectedValue` | `string[]` | No | The controlled selected node value | | `selectionMode` | `'multiple' | 'single'` | No | Whether the tree supports multiple selection - "single": only one node can be selected - "multiple": multiple nodes can be selected | | `showNonenumerable` | `boolean` | No | | | `typeahead` | `boolean` | No | Whether the tree supports typeahead search | | `unmountOnExit` | `boolean` | No | Whether to unmount on exit. | **JsonTreeViewRootProvider Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `value` | `UseTreeViewReturn>` | Yes | | | `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `lazyMount` | `boolean` | No | Whether to enable lazy mounting | | `unmountOnExit` | `boolean` | No | Whether to unmount on exit. | **JsonTreeViewTree Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `arrow` | `ReactElement>` | No | The icon to use for the arrow. | | `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `indentGuide` | `boolean | ReactElement>` | No | The indent guide to use for the tree. | | `renderValue` | `(node: JsonNodeHastElement) => ReactNode` | No | The function to render the value of the node. | #### Solid **JsonTreeViewRoot Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `asChild` | `(props: ParentProps<'div'>) => Element` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `checkedValue` | `string[]` | No | The controlled checked node value | | `collapseStringsAfterLength` | `number` | No | | | `data` | `{}` | No | The data to display in the tree. | | `defaultCheckedValue` | `string[]` | No | The initial checked node value when rendered. Use when you don't need to control the checked node value. | | `defaultExpandedDepth` | `number` | No | The default expand level. | | `defaultExpandedValue` | `string[]` | No | The initial expanded node ids when rendered. Use when you don't need to control the expanded node value. | | `defaultFocusedValue` | `string` | No | The initial focused node value when rendered. Use when you don't need to control the focused node value. | | `defaultSelectedValue` | `string[]` | No | The initial selected node value when rendered. Use when you don't need to control the selected node value. | | `expandedValue` | `string[]` | No | The controlled expanded node ids | | `expandOnClick` | `boolean` | No | Whether clicking on a branch should open it or not | | `focusedValue` | `string` | No | The value of the focused node | | `groupArraysAfterLength` | `number` | No | | | `id` | `string` | No | The unique identifier of the machine. | | `ids` | `Partial<{ root: string; tree: string; label: string; node: (value: string) => string }>` | No | The ids of the tree elements. Useful for composition. | | `lazyMount` | `boolean` | No | Whether to enable lazy mounting | | `loadChildren` | `(details: LoadChildrenDetails) => Promise` | No | Function to load children for a node asynchronously. When provided, branches will wait for this promise to resolve before expanding. | | `maxPreviewItems` | `number` | No | | | `onCheckedChange` | `(details: CheckedChangeDetails) => void` | No | Called when the checked value changes | | `onExpandedChange` | `(details: ExpandedChangeDetails) => void` | No | Called when the tree is opened or closed | | `onFocusChange` | `(details: FocusChangeDetails) => void` | No | Called when the focused node changes | | `onLoadChildrenComplete` | `(details: LoadChildrenCompleteDetails) => void` | No | Called when a node finishes loading children | | `onLoadChildrenError` | `(details: LoadChildrenErrorDetails) => void` | No | Called when loading children fails for one or more nodes | | `onSelectionChange` | `(details: SelectionChangeDetails) => void` | No | Called when the selection changes | | `quotesOnKeys` | `boolean` | No | Whether to show quotes on the keys. | | `selectedValue` | `string[]` | No | The controlled selected node value | | `selectionMode` | `'multiple' | 'single'` | No | Whether the tree supports multiple selection - "single": only one node can be selected - "multiple": multiple nodes can be selected | | `showNonenumerable` | `boolean` | No | | | `typeahead` | `boolean` | No | Whether the tree supports typeahead search | | `unmountOnExit` | `boolean` | No | Whether to unmount on exit. | **JsonTreeViewRootProvider Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `value` | `UseTreeViewReturn>` | Yes | | | `asChild` | `(props: ParentProps<'div'>) => Element` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `lazyMount` | `boolean` | No | Whether to enable lazy mounting | | `unmountOnExit` | `boolean` | No | Whether to unmount on exit. | **JsonTreeViewTree Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `arrow` | `number | boolean | Node | (string & {}) | ArrayElement` | No | The icon to use for the arrow. | | `asChild` | `(props: ParentProps<'div'>) => Element` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `indentGuide` | `number | boolean | Node | (string & {}) | ArrayElement` | No | The indent guide to use for the tree. | | `renderValue` | `(node: JsonNodeHastElement) => Element` | No | The function to render the value of the node. | #### Vue **JsonTreeViewRoot Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `data` | `object` | Yes | The data to display in the tree. | | `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `checkedValue` | `string[]` | No | The controlled checked node values | | `collapseStringsAfterLength` | `number` | No | | | `defaultCheckedValue` | `string[]` | No | The initial checked node values when rendered. Use when you don't need to control the checked node values. | | `defaultExpandedDepth` | `number` | No | The default expand level. | | `defaultExpandedValue` | `string[]` | No | The initial expanded node values when rendered. Use when you don't need to control the expanded node values. | | `defaultFocusedValue` | `string` | No | The initial focused node value when rendered. Use when you don't need to control the focused node value. | | `defaultSelectedValue` | `string[]` | No | The initial selected node values when rendered. Use when you don't need to control the selected node values. | | `expandedValue` | `string[]` | No | The controlled expanded node values | | `expandOnClick` | `boolean` | No | Whether clicking on a branch should open it or not | | `focusedValue` | `string` | No | The id of the focused node | | `groupArraysAfterLength` | `number` | No | | | `id` | `string` | No | The unique identifier of the machine. | | `ids` | `Partial<{ root: string; tree: string; label: string; node(value: string): string }>` | No | The ids of the tree elements. Useful for composition. | | `lazyMount` | `boolean` | No | Whether to enable lazy mounting | | `loadChildren` | `(details: LoadChildrenDetails>) => Promise[]>` | No | A function that loads the children of a node. | | `maxPreviewItems` | `number` | No | | | `quotesOnKeys` | `boolean` | No | Whether to show quotes on the keys. | | `selectedValue` | `string[]` | No | The controlled selected node values | | `selectionMode` | `'multiple' | 'single'` | No | Whether the tree supports multiple selection - "single": only one node can be selected - "multiple": multiple nodes can be selected | | `showNonenumerable` | `boolean` | No | | | `typeahead` | `boolean` | No | Whether the tree supports typeahead search | | `unmountOnExit` | `boolean` | No | Whether to unmount on exit. | **JsonTreeViewRootProvider Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `value` | `TreeViewApi>` | Yes | | | `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `lazyMount` | `boolean` | No | Whether to enable lazy mounting | | `unmountOnExit` | `boolean` | No | Whether to unmount on exit. | **JsonTreeViewTree Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `indentGuide` | `boolean` | No | | #### Svelte **JsonTreeViewRoot Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `asChild` | `Snippet<[PropsFn<'div'>]>` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `checkedValue` | `string[]` | No | The controlled checked node value | | `collapseStringsAfterLength` | `number` | No | | | `data` | `{}` | No | The data to display in the tree. | | `defaultCheckedValue` | `string[]` | No | The initial checked node value when rendered. Use when you don't need to control the checked node value. | | `defaultExpandedDepth` | `number` | No | The default expand level. | | `defaultExpandedValue` | `string[]` | No | The initial expanded node ids when rendered. Use when you don't need to control the expanded node value. | | `defaultFocusedValue` | `string` | No | The initial focused node value when rendered. Use when you don't need to control the focused node value. | | `defaultSelectedValue` | `string[]` | No | The initial selected node value when rendered. Use when you don't need to control the selected node value. | | `expandedValue` | `string[]` | No | The controlled expanded node ids | | `expandOnClick` | `boolean` | No | Whether clicking on a branch should open it or not | | `focusedValue` | `string` | No | The value of the focused node | | `groupArraysAfterLength` | `number` | No | | | `id` | `string` | No | The unique identifier of the machine. | | `ids` | `Partial<{ root: string; tree: string; label: string; node: (value: string) => string }>` | No | The ids of the tree elements. Useful for composition. | | `lazyMount` | `boolean` | No | Whether to enable lazy mounting | | `loadChildren` | `(details: LoadChildrenDetails>) => Promise[]>` | No | Function to load children for a node asynchronously. When provided, branches will wait for this promise to resolve before expanding. | | `maxPreviewItems` | `number` | No | | | `onCheckedChange` | `(details: CheckedChangeDetails) => void` | No | Called when the checked value changes | | `onExpandedChange` | `(details: ExpandedChangeDetails>) => void` | No | Called when the tree is opened or closed | | `onFocusChange` | `(details: FocusChangeDetails>) => void` | No | Called when the focused node changes | | `onLoadChildrenComplete` | `(details: LoadChildrenCompleteDetails>) => void` | No | Called when a node finishes loading children | | `onLoadChildrenError` | `(details: LoadChildrenErrorDetails>) => void` | No | Called when loading children fails for one or more nodes | | `onSelectionChange` | `(details: SelectionChangeDetails>) => void` | No | Called when the selection changes | | `quotesOnKeys` | `boolean` | No | Whether to show quotes on the keys. | | `ref` | `Element` | No | | | `selectedValue` | `string[]` | No | The controlled selected node value | | `selectionMode` | `'multiple' | 'single'` | No | Whether the tree supports multiple selection - "single": only one node can be selected - "multiple": multiple nodes can be selected | | `showNonenumerable` | `boolean` | No | | | `typeahead` | `boolean` | No | Whether the tree supports typeahead search | | `unmountOnExit` | `boolean` | No | Whether to unmount on exit. | **JsonTreeViewRootProvider Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `value` | `UseTreeViewReturn>` | Yes | | | `asChild` | `Snippet<[PropsFn<'div'>]>` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `lazyMount` | `boolean` | No | Whether to enable lazy mounting | | `ref` | `Element` | No | | | `unmountOnExit` | `boolean` | No | Whether to unmount on exit. | **JsonTreeViewTree Props:** | Prop | Type | Required | Description | |------|------|----------|-------------| | `arrow` | `Snippet<[]>` | No | The icon to use for the arrow. | | `asChild` | `Snippet<[PropsFn<'ul'>]>` | No | Use the provided child element as the default rendered element, combining their props and behavior. | | `indentGuide` | `boolean | Snippet<[]>` | No | The indent guide to use for the tree. | | `ref` | `Element` | No | | | `renderValue` | `Snippet<[JsonNodeHastElement]>` | No | The function to render the value of the node. | ## Accessibility The JSON tree view is built on top of the Tree View component and complies with the [Tree View WAI-ARIA design pattern](https://www.w3.org/WAI/ARIA/apg/patterns/treeview/). ### Keyboard Support