JsonViewer

Interactive JSON data viewer with expand/collapse, type indicators and copy to clipboard

License

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:

{
name: "Mantine"
version: "9.7.0"
description: "React components library"
homepage: "https://mantine.dev"
license: "MIT"
devDependencies: null
private: false
sideEffects: false
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:

{
user: {
name: "John Doe"
address: {
street: "123 Main St"
city: "Springfield"
state: "IL"
zip: "62704"
contacts: [
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:

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:

[
1
2
3
4
5
... 7 more items
import { JsonViewer } from '@mantine/code-highlight';
import { data } from './data';

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

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

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

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

Root name

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

response: {
id: 1
name: "Mantine"
type: "library"
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:

{
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:

{object5 keys
name: "Mantine"string
version: 9int
stable: truebool
deprecated: nullnull
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:

{
name: "Mantine"
version: "9.7.0"
description: "React components library"
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 to change that:

{
title: "Short title"
description: "This is a very long descriptio..."
content: "Another long string value that..."
short: "ok"
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:

{
apple: "first"
banana: "second"
mango: "middle"
zebra: "last"
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:

{
name: "@mantine/core"
version: "9.7.0"
description: "React components library"
deprecated: "Use @mantine/core@9 instead"
license: "MIT"
author: {
name: "Vitaly Rtishchev"
url: "https://github.com/rtivital"
repository: {
type: "git"
url: "https://github.com/mantinedev/mantine"
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:

{
user: {
name: "John Doe"
address: {
street: "123 Main St"
city: "Springfield"
contacts: [
{
type: "email"
value: "john@example.com"
{
type: "phone"
value: "+1-555-0123"
settings: {
theme: "dark"
notifications: {
email: true
push: false
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:

{
name: "Mantine"
version: "9.7.0"
description: "React components library"
license: "MIT"
private: false
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:

{
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 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.