JSON Viewer
AlphaJSON Viewer renders JavaScript data as an expandable, collapsible tree with syntax-colored values.
import {JsonViewerModule} from "@qualcomm-ui/angular/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 class="flex w-full min-w-0 flex-col gap-4">
<q-file-input
accept=".json"
class="w-full max-w-sm"
label="JSON File"
placeholder="Select a JSON file"
[disabled]="reading()"
[errorText]="error()"
[invalid]="!!error()"
(fileChanged)="onFileChanged($event)"
/>
@if (result(); as result) {
<div class="max-h-96 overflow-auto">
<div
lazyMount
q-json-viewer
unmountOnExit
[data]="result.data"
[defaultExpandedDepth]="1"
></div>
</div>
} @else if (reading()) {
<p aria-live="polite" class="text-neutral-secondary font-body-sm">
Reading JSON file…
</p>
}
</div>
Simple
Pass any JavaScript value to data. Use defaultExpandedDepth to control how many levels are expanded initially.
<div
q-json-viewer
[collapseStringsAfterLength]="50"
[data]="data"
[defaultExpandedDepth]="0"
[maxPreviewItems]="7"
[quotesOnKeys]="true"
></div>
Data Types
The viewer renders values with type-specific coloring for strings, numbers, booleans, null, undefined, dates, regex, and more.
<div q-json-viewer [data]="data" [defaultExpandedDepth]="2"></div>
Controlled Expansion
Use expandedValue and expandedValueChanged to control expansion. Call collection().getBranchValues() to expand all branches, or set expandedValue to an empty array to collapse them.
<div class="mb-2 flex gap-2">
<button
q-button
(click)="expanded = viewer.collection().getBranchValues()"
>
Expand all
</button>
<button q-button (click)="expanded = []">Collapse all</button>
</div>
<div
#viewer="jsonViewerRoot"
q-json-viewer-root
[data]="data"
[expandedValue]="expanded"
(expandedValueChanged)="expanded = $event.expandedValue"
>
<div q-json-viewer-tree></div>
</div>
Complex Types
Maps, Sets, Headers, URLSearchParams, URLs, Errors, and functions are all supported.
<div class="flex w-full flex-col gap-3">
<label
label="Show expanded brackets"
q-switch
[(ngModel)]="showExpandedBrackets"
></label>
<div
q-json-viewer
[data]="data"
[defaultExpandedDepth]="2"
[showExpandedBrackets]="showExpandedBrackets()"
></div>
</div>
API
q-json-viewer
Apply q-json-viewer to a host element to render the viewer. The component uses the q-json-viewer-root directive and adds the following inputs:
| LucideIcon
| LucideIconData
| string
booleanComposite API
q-json-viewer-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>>
numberbooleanstring[]
| 'single'
| 'multiple'
(
state: NodeState<JsonNode>,
) => boolean | undefined
booleanboolean'sm' | 'md'
booleanboolean{
checkedNodes: Array<JsonNode>
checkedValue: string[]
}
{
expandedNodes: Array<JsonNode>
expandedValue: string[]
focusedValue: string | null
}
{
focusedNode: JsonNode | null
focusedValue: string | null
}
{
collection: TreeCollection<JsonNode>
}
{
nodes: Array<{
error: Error
indexPath: number[]
node: JsonNode
valuePath: string[]
}>
}
{
focusedValue: string | null
selectedNodes: Array<JsonNode>
selectedValue: string[]
}
q-json-viewer-tree
| LucideIcon
| LucideIconData
| string
boolean