# JSON Viewer

> A JSON value as a tree to open, search and walk with the arrow keys, such as a webhook's payload or a tool call's result.

Page: https://uicontext.com/docs/components/json-viewer

Group: Documents.

## When to use

A JSON value as a tree to open, search and walk with the arrow keys, such as a webhook's payload or a tool call's result. Use [Code Block](https://uicontext.com/docs/components/code-block.md) instead when people read the whole payload as text, without folding or searching it.

## Usage

```ts
import { JsonViewer } from "./ui/uicontext/components/JsonViewer.ts"

export const Response = <M>(h: Html.H<M>, body: unknown) =>
	JsonViewer(h, { props: { label: "Response", value: body, depth: 2, searchable: true } })
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### A webhook's payload

The example the page opens with: JSON Viewer in “A webhook's payload”.

```ts title="src/demo.ts" source="www/src/examples/json-viewer/demo.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { JsonViewer } from "./ui/uicontext/components/JsonViewer.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"

const styles = stylex.create({ wrap: { inlineSize: "min(100%, 40rem)" } })

// a payment webhook as it arrived, searchable: try "card", "4242" or "true"
const payload = {
	id: "evt_1Q8vR2",
	type: "invoice.payment_failed",
	created: 1764926043,
	livemode: false,
	data: {
		invoice: {
			id: "in_7HkQ2",
			number: "INV-20419",
			customer: { id: "cus_N8x", name: "Harborview Supply", email: "billing@harborview.example" },
			amount_due: 480000,
			currency: "usd",
			attempt_count: 3,
			next_payment_attempt: null,
			lines: [
				{ description: "Business plan × 12 seats", amount: 432000, period: { start: "2026-12-01", end: "2027-11-30" } },
				{ description: "SSO add-on", amount: 48000, period: { start: "2026-12-01", end: "2027-11-30" } },
			],
			payment_method: { type: "card", card: { brand: "visa", last4: "4242", exp_month: 11, exp_year: 2026 } },
			last_error: { code: "card_declined", decline_code: "expired_card", message: "Your card has expired." },
		},
	},
	request: { id: "req_Z1", idempotency_key: null },
}

export const WebhookPayload = <M>(h: Html.H<M>) =>
	h.div.attrs(sx(styles.wrap)).children(
		JsonViewer(h, { key: "webhook", props: { label: "Webhook payload", value: payload, depth: 3, searchable: true } }),
	)
```

### A tool call's input and result

`depth` sets how much starts open. An array over 100 items opens into ranges of 100.

```ts title="src/tool-call.ts" source="www/src/examples/json-viewer/tool-call.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { JsonViewer } from "./ui/uicontext/components/JsonViewer.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	grid: { display: "grid", gridTemplateColumns: "repeat(auto-fit, minmax(min(100%, 18rem), 1fr))", gap: space.lg, inlineSize: "100%" },
	label: { margin: 0, marginBlockEnd: space.xs, fontSize: font.xs, fontWeight: 600, color: color.muted, textTransform: "uppercase", letterSpacing: "0.04em" },
})

const input = { query: "status:open priority:high", project: "ENG", limit: 50, fields: ["id", "title", "assignee", "updated"] }

// 240 issues come back, so the array opens in ranges of a hundred
const people = ["Ada Okafor", "Tomás García", "Priya Raman", "Wei Chen", null]
const result = {
	total: 240,
	issues: Array.from({ length: 240 }, (_, i) => ({ id: `ENG-${2400 - i}`, title: `Issue ${2400 - i}`, assignee: people[i % people.length], updated: `2026-12-0${(i % 4) + 1}` })),
	truncated: false,
}

// an agent's tool call: its input open to the second level, its long result closed at the top
export const ToolCallDetail = <M>(h: Html.H<M>) =>
	h.div.attrs(sx(styles.grid)).children(
		h.div.children(h.p.attrs(sx(styles.label)).children("Input"), JsonViewer(h, { key: "in", props: { label: "search_issues input", value: input, depth: 2, maxHeight: 280 } })),
		h.div.children(h.p.attrs(sx(styles.label)).children("Result"), JsonViewer(h, { key: "out", props: { label: "search_issues result", value: result, depth: 1, maxHeight: 280 } })),
	)
```

## API reference

**Props**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `label` (required) | `string` | — | What the value is, for screen readers: "Response body". |
| `value` (required) | `unknown` | — | Any JSON value: an object, an array, or a single string, number, boolean or null. |
| `depth` | `number` | `1` | How many levels start open; 1 by default. |
| `searchable` | `boolean` | — | A search field and Expand all and Collapse all above the tree. |
| `maxHeight` | `number` | `360` | The tree's tallest, in px, before it scrolls; 360 by default. |

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/json-viewer
```

With npm: `npx shadcn@latest add checkfu/uicontext/json-viewer`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/json-viewer`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/json-viewer`.

It writes these files:

- `src/ui/uicontext/components/Direction.ts`
- `src/ui/uicontext/components/Glyph.ts`
- `src/ui/uicontext/components/IconStroke.ts`
- `src/ui/uicontext/components/JsonViewer.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/TreeView.ts`
- `src/ui/uicontext/components/domain/Format.ts`
- `src/ui/uicontext/components/domain/Glyph.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/JsonViewer.ts](https://github.com/checkfu/uicontext/blob/main/src/JsonViewer.ts)

## Related

- [Editor](https://uicontext.com/docs/components/editor.md) (`editor`): A block editor with a slash menu for headings, lists, to-dos and more.
- [Document Blocks](https://uicontext.com/docs/components/document-blocks.md) (`document-blocks`): Read-only rendering of a document's blocks.
- [Markdown](https://uicontext.com/docs/components/markdown.md) (`markdown`): Renders Markdown with readable type.
- [Code Block](https://uicontext.com/docs/components/code-block.md) (`code-block`): Code to read and copy, with highlighting, a file name and line numbers.
- [Log Viewer](https://uicontext.com/docs/components/log-viewer.md) (`log-viewer`): A log that follows its newest line, with a filter, level buttons and Wrap, such as a deploy's or a server's.
- [Diff](https://uicontext.com/docs/components/diff.md) (`diff`): Added and removed lines in a change, side by side or inline.
