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.
- 8.8 kB JS 4.1 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- Right-to-left
- 14 tests
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: "[email protected]" }, 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 } }), )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 instead when people read the whole payload as text, without folding or searching it.
Usage
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
A tool call's input and result
depth sets how much starts open. An array over 100 items opens into ranges of 100.
Input
Result
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 hundredconst 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 topexport 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 |
|---|---|---|---|
labelrequired | string | — | What the value is, for screen readers: "Response body". |
valuerequired | 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
pnpm dlx shadcn@latest add checkfu/uicontext/json-viewer