# JSON inspector

> Read, search and quote a webhook payload or an API response, or see exactly what changed between two versions of a record.

Payloads usually land on screen as a wall of pretty-printed text: you scroll, you squint, you count brackets to find which "id" you're looking at, and you retype its path by hand. Here objects and arrays fold open by height, and closed ones show what's inside ({ id, amount, … } or 2 items). Search finds keys and values at once; the branches above every match unfold, the count rolls, and Enter steps from match to match and scrolls each into view. The row you're on shows its whole string, says when a timestamp is, and its path is written out underneath (data.object.lines.data[0].amount), morphing letter by letter as you move, with Copy path and Copy value beside it. Give it the value before and it becomes a diff: added keys are marked, removed ones struck through, changed values sit struck out beside the new, and everything that didn't change folds into "12 unchanged". Text that isn't JSON points at the line and column where it broke.

## Install

```bash
npx shadcn@latest add @hairline/json-inspector
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: none
- Hairline UI components it needs (the CLI installs them too): `text-morph`, `number-roll`, `status-button`
- Docs and live preview: https://hairlineui.com/ui/json-inspector
- Registry item (source): https://hairlineui.com/r/json-inspector.json

## Usage

```tsx
import { JsonInspector } from "@/components/json-inspector";

<JsonInspector title="invoice.paid" data={event} />
```

## Webhook deliveries and an audit log

Show the body your endpoint received as text, so a malformed delivery says where it broke. For an audit log, pass the record before and after a change; match array items by their id so an inserted item doesn't mark every row below it as changed.

```tsx
// app/webhooks/[id]/delivery.tsx
import { JsonInspector } from "@/components/json-inspector";

export async function Delivery({ id }: { id: string }) {
  const delivery = await db.webhookDelivery.findUniqueOrThrow({ where: { id } });
  return (
    <JsonInspector
      label="Request body"
      title={`${delivery.event} · ${delivery.status} in ${delivery.ms} ms`}
      text={delivery.body}
      pathStyle="jq"
    />
  );
}

// app/audit/[id]/change.tsx
export async function Change({ id }: { id: string }) {
  const event = await db.auditEvent.findUniqueOrThrow({ where: { id }, include: { actor: true } });
  return (
    <JsonInspector
      label="Change"
      title={`${event.actor.name} ${event.action}`}
      compare={event.before}
      data={event.after}
      arrayKey="id"
    />
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `data` | `unknown` | – | A value to show. Circular references show as "[circular]". |
| `text` | `string` | – | JSON text instead of a value; if it doesn't parse, the error is shown with the line and column. |
| `compare` | `unknown` | – | The value before. With it, the inspector shows how compare became data. |
| `arrayKey` | `string` | – | In a diff, match array items by this key ("id") instead of by position. |
| `expandDepth` | `number` | `2` | Levels open at first (in a diff, everything with changes opens instead). |
| `title` | `ReactNode` | – | Shown at the top left, e.g. the event name and status. |
| `defaultQuery` | `string` | – | Start with this search, e.g. from a "find in payload" link. |
| `pathStyle` | `"js" \| "jq"` | `"js"` | How paths are written: data.items[0].id, or .data.items[0].id for jq. |
| `maxHeight` | `number` | `360` | Height of the scrolling body in px. |
| `label` | `string` | `"JSON"` | Accessible name of the tree. |

## States

- **fold**: A container opens and closes by height (grid rows, 280ms) while its chevron turns 90°. Children stay mounted once shown, so closing animates as well as opening. Closed containers preview their first three keys or their item count.
- **focus**: The row you're on (clicked or reached with the arrows) takes the accent fill, shows its whole string instead of a truncated one, and adds a readable date beside ISO strings and Unix timestamps under keys like created or period_end.
- **path**: The footer writes the path of that row and morphs it letter by letter as you move (360ms), so the shared prefix stays put. Copy path and Copy value are Status buttons: the label becomes Copied and a check draws itself.
- **search**: Matches are marked in keys and values 120ms after typing stops. Branches above every match open; the "3/12" count opens in beside the field and rolls; Enter, ↓ or the arrows step to the next match, which is outlined and scrolled to a third of the way down.
- **diff**: With compare, each row is same, added (+, emerald tint), removed (−, red tint, struck) or changed (~, old value struck beside the new). Containers with changes open; closed ones show their counts. Unchanged keys fold into a "12 unchanged" row until you ask for them or a search finds one.
- **broken**: Text that doesn't parse shows the reason, the line and column, and the lines around it with a caret under the exact character, found by its own scanner because browsers report positions differently.

## What it handles

Payloads usually land on screen as pretty-printed text: people scroll, count brackets to find which “id” they’re looking at, and retype its path by hand. This turns a payload into something you can read, search and quote.

- **Folds and previews**: Objects and arrays open and close; closed ones show what’s inside, like { id, amount, … } or 12 items.
- **Searches keys and values**: The branches above every match open, and Enter steps from one match to the next.
- **Copies the path**: The row you’re on is written out, as data.items[0].id or in jq style, with Copy path and Copy value beside it.
- **Reads timestamps**: ISO dates and Unix times under keys like created_at say when they are.
- **Diffs two versions**: Added, removed and changed keys are marked, arrays are matched by id, and unchanged keys fold away.
- **Explains broken JSON**: It points at the line and column where parsing stopped, with the lines around it.

## Replaces

JSON.stringify(data, null, 2), react-json-view, a diff library, a JSONPath builder, a JSON error parser

## Where it fits

- **Webhook deliveries**: Show the payload your endpoint received, and exactly where a broken one broke.
- **API explorers**: Let developers read responses in your dashboard or docs and copy what they need.
- **Audit logs**: Show which fields changed between two versions of a record, and nothing else.
- **AI tool calls**: Inspect the arguments and results of tool calls without scrolling a wall of text.

## Notes

- A real tree for assistive tech: treeitems with level, position and expanded state, one tab stop. ↑ and ↓ move, → opens or steps in, ← closes or steps out, Home and End jump, Enter or Space toggles, / jumps to search, and ⌘C or Ctrl+C copies the value of the row you're on.
- Each row has a spoken label (key, value, and in a diff "added", "removed" or "was …"); match counts and copies are announced in a status region.
- Containers with more than 100 children show them a hundred at a time with "Show 100 more · 1,240 left"; a search reaches past that to show a match.
- json-tree.ts has no React in it: buildTree, diffTree, formatPath, searchTree and parseError work on a server too, e.g. to store the diff of an audit event.
- Strings, numbers and booleans use the same light and dark palette as Code block; diff marks use emerald, red and amber with a sign beside each, so colour is never the only signal.
- Reduced motion: folds, the chevron and the path change in place.

License: MIT (https://hairlineui.com/license#components)
