JSON viewer

A JSON document as a tree you can open and close: a webhook payload, an API response, an audit log entry, a job's arguments.

JSON viewer

Needs Alpine and the package's script for the tree's keys, Expand all and the per-node copy actions. See getting started. Without them it is complete: every node is a native details that opens and closes with no script, and the header's copy button needs Alpine only.

Use it

<x-ds::json-viewer label="invoice.paid" :data="$event->payload" />

Or a JSON string, decoded on the server:

<x-ds::json-viewer label="Response" :json="$log->response_body" :maxHeight="20" />

A string that is not valid JSON shows the parser's message and the text as it came, so a bad payload is still readable.

Props

PropTypeDefaultWhat it does
datamixed—The value: an array, an object (JsonSerializable and Arrayable too) or a scalar. Give this or json, not both.
jsonstring—A JSON string, decoded here.
expandint, true1How many levels open at first. 0 shows only the top row; true (or 'all') opens everything.
labelstringJSONThe header's words, and the scroll region's name.
copybooltrueA copy button in the header for the whole document and, with the script, Copy value and Copy path on every node.
maxHeightint—At most this many lines tall; the rest scrolls inside.
rootNamestring—What the document is called in a copied path (data gives data.items[3].id) and on its top row.
sortKeysboolfalseObject keys in alphabetical order rather than as they came.
truncateint, null200Strings longer than this are cut, with a "Show more". null never cuts.
maxItemsint, null100Items shown in one object or array; the rest wait behind "Show N more".
maxNodesint2000The most nodes drawn in the page. Past it, see Large documents.
linkifyboolfalsehttp and https addresses in strings become links, opening in a new tab with rel="noopener noreferrer".

What it shows

  • Keys unquoted (an empty key as ""), strings quoted, null, true and false as words. Colour is never the only sign of a type.
  • A closed node shows its bracket and a count: lines: […] 3 items, meta: {…} 4 keys.
  • Colours are the code block's token colours, so a viewer and a highlighted block beside it agree, in both themes.
  • Everything is escaped. Markup in a string is shown as text.
  • JSON reads left to right on a right-to-left page too: the body is dir="ltr".

With the script

  • Keys. The rows are one Tab stop, as in a tree: Up and Down, Right opens and goes in, Left closes and goes out, Home, End, * opens every node beside this one, Enter and Space open and close, and typing jumps to a key.
  • Expand all and Collapse all in the header. Collapse all leaves the document itself open. Neither touches a long string or a "Show N more".
  • Copy value and Copy path, on the row under the pointer or holding focus. A string copies as its text; anything else as indented JSON. The path is JavaScript-style: data.items[3]["first name"]. "Copied" is announced.

Large documents

Every node is in the page, so a very large document is a very large page. maxItems keeps long arrays short (the rest sit in a closed "Show N more", still in the page). maxNodes is the hard stop: nodes past it are drawn as stubs, closed, with nothing inside them.

Opening a stub dispatches expand with stub: true, for a host that wants to load the rest:

<div x-on:expand="if ($event.detail.stub) $wire.loadNode($event.detail.keys)">
    <x-ds::json-viewer :data="$payload" :maxNodes="500" />
</div>

Events

expand and collapse bubble from a node the reader opens or closes, with detail { path, keys, stub }: path is the copied path's text, keys the list of keys from the top (strings and numbers), stub true past maxNodes. Expand all and Collapse all send none: they are not the reader asking about one node.

Accessibility

  • Each node is a native details; its summary reads as the key, the type and the count: "lines, array, 3 items".
  • The body is a named region that takes focus, so a keyboard can scroll it.
  • With the script the rows are one Tab stop with the tree's keys; the copy actions follow focus and are real buttons.

Don't

  • Don't use it for a document people edit. It is a reader. Use a textarea or a code editor.
  • Don't pass secrets through it. What it shows is in the page source.
  • Don't raise maxNodes to draw a huge document. Load the rest on expand, or link to the raw file.

More in specs/json-viewer.md.


From the package repo: edit this page on GitHub.

Was this page helpful?