JSON viewer

A JSON document drawn on the server as nested native details, with the tree's keys, Expand all and per-node copying added by the script.

Anatomy

┌ invoice.paid ─────────────── [Expand all] [Collapse all] [⧉] ┐  figcaption
│ ▾ event: {                                     5 keys          │  summary (row)
│     id: "evt_1Nx42"                                             │  leaf (row)
│   ▸ lines: […]                                 3 items  [⧉][🔗]│  closed node + actions bar
│     description: "Pro plan, monthl…" Show more                  │  long string, cut
│   }                                                             │  close bracket
└─────────────────────────────────────────────────────────────────┘
   radius-panel · border · the code block's --ds-code-bg
PartTokenWhy
Ground and text--ds-code-bg, --ds-code-fg on [data-ds-code]The code block's palette, so a viewer beside a highlighted block agrees, both themes.
Key--ds-code-parameter
String--ds-code-string, quotedColour is never the only sign of a type.
Number--ds-code-constant
true false null--ds-code-keyword; null italicWords, not colours alone.
Brackets--ds-code-punctuation
Counttext-meta, fg-muted, sansReads as a note, not as data.
Guide lineborder-divider, start sideShows which bracket a child belongs to.
Row focusfocus-ring, insetThe scroll box is tight; an outer ring would be clipped.

Rendering

Support\Json::render() writes the tree as one PHP string from the class recipe in resources/views/partials/json.php (a consumer's Tailwind scans resources/views, never src/). A recursive Blade partial was the alternative: thousands of view renders for a large document, and Livewire's morph markers inside every one.

  • Normalised first. The value goes through json_encode and back, objects as stdClass, so a model, a collection or a JsonSerializable shows exactly what json_encode would send, and the copied text is that document.
  • Keys as given. Each node's path is a JSON array of keys on its details or leaf (data-ds-json-path). A string key "3" stays a string: entries are walked as pairs, never through PHP's integer-key coercion.
  • Escaped. Every key and value is escaped; linkify wraps only the escaped text of an http(s) address.

The budget

maxNodes is planned breadth-first: nodes are admitted level by level until the next container does not fit, and from there on every container is a stub: a closed details[data-ds-json-stub] with its key and count and nothing inside. Breadth-first because the top of a document is what a reader opens first; a depth-first cut would draw one deep branch completely and leave the rest of the top level empty.

maxItems past which children sit in a closed details[data-ds-json-rest] "Show N more" is not lazy: those children are in the page and count against the budget. A template would have saved the nodes and cost the no-script viewer, which is complete today.

Open and closed

Native details, so it opens and closes with no script. What shows closed (…}, the count) and open (the children, the closing bracket) is chosen by two custom properties each details sets for itself, --ds-closed and --ds-opened, read as display: var(…). A group-open: variant matches any open ancestor, so a closed node inside an open one read as open; a custom property inherits from the nearest details only.

The disclosure triangle is the browser's own list-item marker, which follows its own details and the reading direction with no help. A leaf draws the same marker in no colour, so its key lines up with its siblings'.

The script (x-ds-json, an enhancement)

  • The rows (summaries and leaves) take the tree's keys, shared/tree-nav.js, the same module as ds::tree and the tree table.
  • A press on a summary goes through the script, so it can say expand.
  • Expand all, Collapse all: rendered hidden, shown by the script. Over every drawn node, never a stub, a long string or a "Show N more". Collapse all leaves the root open. Neither dispatches events.
  • Actions: one bar, moved into the node under the pointer or holding focus. Copy value walks the parsed source (pre[data-ds-json-source]) by the node's keys: a string as its text, anything else JSON.stringify(…, 2). Copy path writes root.name[3]["a b"]: identifiers dotted, numbers and other keys bracketed.
  • Events: expand and collapse from a node the reader opens or closes, bubbling, { path, keys, stub }.

Accessibility

Bodyrole="region", named by label, tabindex="0" to scroll by keyboard, dir="ltr"
Nodedetails / summary; the summary reads "lines, array, 3 items" (sr-only ", array, " between key and count)
Rows with the scriptone Tab stop, the tree's keys
Copyreal buttons with names; "Copied" in a polite live region

Do not

  • Do not render it with a recursive Blade partial. See Rendering.
  • Do not cut the budget depth-first. See The budget.
  • Do not use group-open: for what shows open. See Open and closed.
  • Do not copy from the DOM. Truncated strings and stubs are not the document; copy from the source.

From the package repo: edit this page on GitHub.

Was this page helpful?