Skip to content
EspañolEnglish
All components (283)

JSON viewer

Data · JsonViewer · .amset-jsonviewer

What it is and when to use it

A JSON to read: it opens and closes by levels, the values come out in the colour of their type and the whole thing can be copied.

When to use it

  • Showing the response of an API, an audit record or the content of a message to whoever reviews it.
  • value is any value: an object, a list or a single value. Dates, bigint, undefined and circular references come out fine.
  • defaultOpenDepth says how many levels start open (1 by default; with Infinity, all of them), maxItems how many entries are drawn before "N more" and sortKeys sorts the keys.
  • Pressing a key copies its path (customer.address.city, lines[0].sku).
  • "Expand all" and "Collapse all" open or fold every level at once.
  • The top bar shows the path of the key under the pointer or with focus (customer.address.city), without having to copy it.

When not to

  • Code that is written or copied with its lines: CodeBlock.
  • Data that is edited: a form.
  • Data compared in columns: Table.

Keyboard: each level is a browser <details>: Tab moves from level to level and Enter or Space opens and closes it. The page finder (Ctrl+F) opens the level where it finds the text.

Accessibility: it is a group named after label; each level says whether it is open or closed and how many keys or items it has. Colour is not the only thing that tells a type apart: strings carry quotes and empty values are written out (null, undefined).

Fine-tuning: --amset-jsonviewer-font, --amset-jsonviewer-indent and the colours --amset-jsonviewer-key, -string, -number and -keyword.

What it looks like

{8 keys}
: "PED-2041"
: "paid"
: 128.5
: false
: null
customer: {3 keys}
: "Marta Ruiz"
: "marta@example.com"
address: {2 keys}
: "Seville"
: "41001"
}
}
lines: [2 items]
0: {3 keys}
: "CAM-001"
: 2
: 24.9
}
1: {3 keys}
: "GOR-014"
: 1
: 78.7
}
]
: []
}

<JsonViewer label="Order response" value={pedido} />

Props

Import it with import { JsonViewer } from '@amset/ui'. Required props are marked.

PropTypeWhat it does
labelrequiredstringWhat this JSON is: "Order response". Required: it is the name of the region.
valuerequiredunknownWhat is shown: an object, a list or a single value.
classNamestringMerged with the library classes; it does not replace them.
defaultOpenDepthnumberHow many levels start open. 1 by default. With `Infinity`, all of them.
maxItemsnumberHow many entries of each object or list are drawn before saying "N more". 100 by default.
sortKeysbooleanSorts the keys of the objects alphabetically.
withoutCopybooleanRemoves the copy button.

It also accepts the attributes of the element it renders: className, style, data-*, aria-* and its events.

CSS variables

These CSS variables are how you adapt it. Declare them on the component's selector, not on :root: the stylesheet redeclares them in the rule itself, and a declaration on the same element always wins.

VariableDefault
--amset-jsonviewer-fontvar(--amset-text-sm)
--amset-jsonviewer-indent1.25rem
--amset-jsonviewer-radiusvar(--amset-radius)
--amset-jsonviewer-keyvar(--amset-accent-text)
--amset-jsonviewer-stringvar(--amset-success)
--amset-jsonviewer-numbervar(--amset-warning)
--amset-jsonviewer-keywordvar(--amset-secondary-text)

How to change it

.amset-jsonviewer {
  --amset-jsonviewer-font: ...;
  --amset-jsonviewer-indent: ...;
}

In the test bench

Every state and variant, to try one at a time, on its page at storybook.amsetui.com: