JSON Viewer
JsonViewer renders a value as a collapsible JSON tree in your theme's tokens. getHref turns object IDs into links, and timestamps show their date on hover. Hover created, or click the customer ID.
@dentil/json-viewer'use client';
import NextLink from 'next/link';
import { JsonViewer } from '@/components/dentil/json-viewer/json-viewer';
const event = {
id: 'evt_1QxP4nLk9mT8pR7s',
object: 'event',
type: 'invoice.paid',
created: 1_794_042_840,
livemode: false,
data: {
object: {
id: 'inv_000123',
object: 'invoice',
number: 'INV-0123',
customer: 'cus_004',
status: 'paid',
amount_due: 12_500,
amount_paid: 12_500,
currency: 'usd',
due_date: 1_795_252_440,
paid_at: '2026-11-07T09:14:00Z',
lines: [
{
description: 'Pro plan',
quantity: 1,
unit_amount: 12_500,
period: { start: 1_794_042_840, end: 1_796_634_840 },
},
],
discount: null,
metadata: {},
},
},
};
/** IDs with a page in your app. Unknown prefixes stay plain text. */
const routes: Record<string, string> = {
cus_: '/customers',
inv_: '/invoices',
};
function getHref(value: string) {
const prefix = Object.keys(routes).find((candidate) => value.startsWith(candidate));
// In your app, return `${routes[prefix]}/${value}`.
return prefix ? '/docs/json-viewer#links' : undefined;
}
export function JsonViewerOverviewDemo() {
return (
<JsonViewer data={event} getHref={getHref} linkComponent={NextLink} timezone="Europe/London" className="w-full" />
);
}Installation
pnpm dlx shadcn@latest add @dentil/json-viewernpx shadcn@latest add @dentil/json-vieweryarn dlx shadcn@latest add @dentil/json-viewerbunx --bun shadcn@latest add @dentil/json-viewerFiles land in components/dentil/json-viewer/. It uses the stock HoverCard and the date formatting from @dentil/date, which the CLI installs with it. There is no provider and no JSON library.
Usage
import { JsonViewer } from '@/components/dentil/json-viewer/json-viewer';
<JsonViewer data={event} timezone="Europe/London" />Click an object or array to collapse it to a count of its keys or items. Keys are muted so the values read first. Quotes, colons and commas are real text, so selecting an expanded tree copies valid JSON.
Examples
Collapsed
defaultExpandDepth={1} shows the top-level keys and collapses everything under them. Expand body to see its nested address stay collapsed.
'use client';
import { JsonViewer } from '@/components/dentil/json-viewer/json-viewer';
const request = {
method: 'POST',
path: '/v1/customers',
status: 200,
duration_ms: 182,
headers: {
'content-type': 'application/json',
'idempotency-key': '8c1f0b6e-7a52-4d3e-9a0f-3b2d6c41e9a7',
},
body: {
name: 'Ada Lovelace',
email: 'ada@example.com',
address: { line1: '12 Analytical Row', city: 'London', postal_code: 'N1 9GU', country: 'GB' },
},
tags: ['api', 'live'],
};
export function JsonViewerCollapsedDemo() {
return <JsonViewer data={request} defaultExpandDepth={1} className="w-full" />;
}Links
getHref receives every string value and its key. Match the ID prefixes your API uses and return the page for each. Values without a match stay plain text. Pass linkComponent so links use your router.
import Link from 'next/link';
const routes: Record<string, string> = {
cus_: '/customers',
inv_: '/invoices',
};
function getHref(value: string) {
const prefix = Object.keys(routes).find((candidate) => value.startsWith(candidate));
return prefix ? `${routes[prefix]}/${value}` : undefined;
}
<JsonViewer data={event} getHref={getHref} linkComponent={Link} />Timestamps
By default, ISO 8601 date-times and unix timestamps between 2000 and 2100, in seconds or milliseconds, are underlined with a dotted line. A number counts when its key looks like a date (created, paid_at, dueDate) and never when it looks like money or a count (amount, quantity). With any other key, a number counts only in seconds, and a numeric string doesn't count. Pass a function to timestamps to change the rule.
import { isTimestamp } from '@/components/dentil/json-viewer/timestamps';
// Also treat `billed` as a timestamp.
<JsonViewer data={event} timestamps={(value, key) => key === 'billed' || isTimestamp(value, key)} />
// Turn it off.
<JsonViewer data={event} timestamps={false} />API reference
JsonViewer
| Prop | Type | Default | Description |
|---|---|---|---|
| data | unknown | — | Any JSON-serializable value. As with JSON.stringify, undefined and functions are skipped in objects and shown as null in arrays. A Date renders as its ISO string. |
| defaultExpandDepth | number | Infinity | Objects and arrays nested shallower than this start expanded. 0 collapses the root, and 1 shows only its keys. It is read once per node, so change the key to reset. |
| getHref | (value: string, key: string | number | undefined) => string | undefined | — | Called for every string value with its object key or array index. Return a URL to render the value as a link, or undefined to leave it as text. |
| linkComponent | ElementType<{ href: string; className?: string; children?: ReactNode }> | 'a' | Renders the links from getHref. Pass next/link, or adapt your router's link once. |
| timestamps | boolean | ((value: string | number, key: string | number | undefined) => boolean) | true | Underlines timestamps and shows the date in timezone and UTC on hover. true uses isTimestamp. A function decides per value, and false turns it off. |
| timezone | string | — | IANA time zone shown beside UTC in the hover card, such as Europe/London. Omit it to use the runtime zone. An unknown name is shown as UTC. |
| locale | string | — | BCP 47 language tag for the hover card dates. Omit it to use the runtime locale. |
| className | string | — | Merged onto the root, which is font-mono text-xs leading-5. Override the size here. |
| …props | Omit<ComponentProps<'div'>, 'children'> | — | Other props are passed to the root div. |
isTimestamp
isTimestamp(value, key) from @/components/dentil/json-viewer/timestamps is the default rule. parseTimestamp(value) from the same file returns the Date it refers to, or null.
| Prop | Type | Default | Description |
|---|---|---|---|
| value | string | number | — | A string or number value from the data. |
| key | string | number | undefined | — | Its object key, array index, or undefined for the root value. |