dentil

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
On this page
"id": "evt_1QxP4nLk9mT8pR7s",
"object": "event",
"type": "invoice.paid",
"created": ,
"livemode": false,
"id": "inv_000123",
"object": "invoice",
"number": "INV-0123",
"customer": "cus_004",
"status": "paid",
"amount_due": 12500,
"amount_paid": 12500,
"currency": "usd",
"due_date": ,
"paid_at": "",
"description": "Pro plan",
"quantity": 1,
"unit_amount": 12500,
"start": ,
"end":
}
}
],
"discount": null,
"metadata": {}
}
}
}

Installation

pnpm dlx shadcn@latest add @dentil/json-viewer
npx shadcn@latest add @dentil/json-viewer
yarn dlx shadcn@latest add @dentil/json-viewer
bunx --bun shadcn@latest add @dentil/json-viewer

Files 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.

"method": "POST",
"path": "/v1/customers",
"status": 200,
"duration_ms": 182,
}

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

PropTypeDefaultDescription
dataunknown—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.
defaultExpandDepthnumberInfinityObjects 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.
linkComponentElementType<{ href: string; className?: string; children?: ReactNode }>'a'Renders the links from getHref. Pass next/link, or adapt your router's link once.
timestampsboolean | ((value: string | number, key: string | number | undefined) => boolean)trueUnderlines timestamps and shows the date in timezone and UTC on hover. true uses isTimestamp. A function decides per value, and false turns it off.
timezonestring—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.
localestring—BCP 47 language tag for the hover card dates. Omit it to use the runtime locale.
classNamestring—Merged onto the root, which is font-mono text-xs leading-5. Override the size here.
…propsOmit<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.

PropTypeDefaultDescription
valuestring | number—A string or number value from the data.
keystring | number | undefined—Its object key, array index, or undefined for the root value.