JSON Viewer
AlphaJSON Viewer renders JavaScript data as an expandable, collapsible tree with syntax-colored values.
import {JsonViewer} from "@qualcomm-ui/react/json-viewer"Examples
Interactive
Select a .json file with File Input to view its contents as a tree. Invalid JSON displays an error. Clear the selected file to reset the viewer.
This example is intended for smaller JSON files, typically under 100 KB. The lazyMount option reduces initial rendering work for larger files by deferring branch content until expansion. Expanding all nodes programmatically renders the entire tree and can reduce responsiveness for large datasets.
<div className="flex w-full min-w-0 flex-col gap-4">
<FileInput
accept=".json"
className="w-full max-w-sm"
disabled={reading}
errorText={error}
invalid={!!error}
label="JSON File"
onFileChange={onFileChange}
placeholder="Select a JSON file"
/>
{result ? (
<div className="max-h-96 overflow-auto">
<JsonViewer
data={result.data}
defaultExpandedDepth={1}
lazyMount
unmountOnExit
/>
</div>
) : reading ? (
<p aria-live="polite" className="text-neutral-secondary font-body-sm">
Reading JSON file…
</p>
) : null}
</div>
Simple
Pass any JavaScript value to data. Use defaultExpandedDepth to control how many levels are expanded initially.
<JsonViewer
collapseStringsAfterLength={50}
data={data}
defaultExpandedDepth={0}
maxPreviewItems={7}
quotesOnKeys
/>
Data Types
The viewer renders values with type-specific coloring for strings, numbers, booleans, null, undefined, dates, regex, and more.
<JsonViewer data={data} defaultExpandedDepth={2} />
Root Provider
Use useJsonViewer with JsonViewer.RootProvider to control expansion through expandedValue and onExpandedValueChange. Call collection.getBranchValues() to expand all branches, or set expandedValue to an empty array to collapse them.
<JsonViewer.RootProvider
expandedValue={expanded}
onExpandedValueChange={({expandedValue}) => setExpanded(expandedValue)}
value={viewer}
>
<JsonViewer.Tree arrow={ChevronRight} indentGuide />
</JsonViewer.RootProvider>
Complex Types
Maps, Sets, Headers, URLSearchParams, URLs, Errors, and functions are all supported.
<div className="flex w-full flex-col gap-3">
<Switch
checked={showExpandedBrackets}
label="Show expanded brackets"
onCheckedChange={setShowExpandedBrackets}
/>
<JsonViewer
data={data}
defaultExpandedDepth={2}
showExpandedBrackets={showExpandedBrackets}
/>
</div>
API
<JsonViewer>
The simple JSON viewer extends the JsonViewer.Root component with the following props:
| LucideIcon
| ReactNode
booleanJsonViewer.Tree element.Composite API
<JsonViewer.Root>
unknownstring[]
numberstring[]
numberdefaultExpandedValue.Ignored when
defaultExpandedValue is provided.string[]
string | null
string[]
'ltr' | 'rtl'
string[]
booleanstring | null
() =>
| ShadowRoot
| Document
| Node
numberstringboolean(details: {
indexPath: number[]
node: JsonNode
signal: AbortSignal
valuePath: string[]
}) => Promise<Array<JsonNode>>
number(details: {
checkedNodes: Array<JsonNode>
checkedValue: string[]
}) => void
(details: {
expandedNodes: Array<JsonNode>
expandedValue: string[]
focusedValue: string | null
}) => void
(details: {
focusedNode: JsonNode | null
focusedValue: string | null
}) => void
(details: {
collection: TreeCollection<JsonNode>
}) => void
(details: {
nodes: Array<{
error: Error
indexPath: number[]
node: JsonNode
valuePath: string[]
}>
}) => void
(details: {
focusedValue: string | null
selectedNodes: Array<JsonNode>
selectedValue: string[]
}) => void
boolean| ReactElement
| ((
props: object,
) => ReactElement)
string[]
| 'single'
| 'multiple'
(
state: NodeState<T>,
) => boolean | undefined
booleanboolean'sm' | 'md'
booleanboolean<JsonViewer.RootProvider>
UseJsonViewerReturnuseJsonViewer. Use this when you need to
access the viewer state outside the component tree.string[]
string[]
string[]
string | null
string[]
'ltr' | 'rtl'
string[]
booleanstring | null
() =>
| ShadowRoot
| Document
| Node
stringboolean(details: {
indexPath: number[]
node: JsonNode
signal: AbortSignal
valuePath: string[]
}) => Promise<Array<JsonNode>>
(details: {
checkedNodes: Array<JsonNode>
checkedValue: string[]
}) => void
(details: {
expandedNodes: Array<JsonNode>
expandedValue: string[]
focusedValue: string | null
}) => void
(details: {
focusedNode: JsonNode | null
focusedValue: string | null
}) => void
(details: {
collection: TreeCollection<JsonNode>
}) => void
(details: {
nodes: Array<{
error: Error
indexPath: number[]
node: JsonNode
valuePath: string[]
}>
}) => void
(details: {
focusedValue: string | null
selectedNodes: Array<JsonNode>
selectedValue: string[]
}) => void
| ReactElement
| ((
props: object,
) => ReactElement)
string[]
| 'single'
| 'multiple'
(
state: NodeState<T>,
) => boolean | undefined
'sm' | 'md'
booleanboolean<JsonViewer.Tree>
| LucideIcon
| ReactNode
boolean(
node: JsonNodeHastElement,
) => ReactNode
useJsonViewer
unknownstring[]
numberstring[]
numberdefaultExpandedValue.Ignored when
defaultExpandedValue is provided.string[]
string | null
string[]
'ltr' | 'rtl'
string[]
booleanstring | null
() =>
| ShadowRoot
| Document
| Node
numberboolean(details: {
indexPath: number[]
node: JsonNode
signal: AbortSignal
valuePath: string[]
}) => Promise<Array<JsonNode>>
number(details: {
checkedNodes: Array<JsonNode>
checkedValue: string[]
}) => void
(details: {
expandedNodes: Array<JsonNode>
expandedValue: string[]
focusedValue: string | null
}) => void
(details: {
focusedNode: JsonNode | null
focusedValue: string | null
}) => void
(details: {
collection: TreeCollection<JsonNode>
}) => void
(details: {
nodes: Array<{
error: Error
indexPath: number[]
node: JsonNode
valuePath: string[]
}>
}) => void
(details: {
focusedValue: string | null
selectedNodes: Array<JsonNode>
selectedValue: string[]
}) => void
booleanstring[]
| 'single'
| 'multiple'
(
state: NodeState<T>,
) => boolean | undefined
booleanbooleanbooleanbooleanTreeCollection<JsonNode>
data. Use for programmatic tree
operations like querying branch values for expand/collapse control.| string[]
| undefined
defaultExpandedValue when
provided, otherwise derives the values from defaultExpandedDepth.
Use as the initial controlled expandedValue.QdsJsonViewerOptionsJsonViewer.RootProvider to configure display formatting.Omit<
TreeApiProps<JsonNode>,
'collection'
> &
RenderStrategyApiProps
JsonViewer.RootProvider and forwarded to the underlying tree root.