JSON Viewer

Alpha

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

Select a JSON file
<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.

{ array: Array(3), boolean: true, float: 3.14159, … }
array : (3) [ 1, "two", false ]
0 : 1
1 : "two"
2 : false
length : 3
boolean : true
float : 3.14159
nested : { date: Wed Jan 01 2025 00:00:00 GMT+0000 (Coordinated Universal Time), regex: /[a-z]+/gi, symbol: Symbol(example) }
date : 2025-01-01T00:00:00.000Z
symbol : Symbol(example)
null : null
number : 42
string : "hello world"
undefined : undefined
<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.

0 of 4 branches expanded
<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.

{ binary: ArrayBuffer(16), booleanStack: Object, callback: ƒ callback(), … }
binary : ArrayBuffer(16)
booleanStack : { stack: false }
stack : false
callback : ƒ ()
name : "callback"
length : 0
constructor : "Function"
[[Function]] : "function(){}"
dateMap : Map(2) { "valid" => 2025-01-01T00:00:00.000Z , "invalid" => Invalid Date }
size : 2
dateSet : Set(2) { 2025-01-01T00:00:00.000Z , Invalid Date }
size : 2
errors : (2) [ Error: Connection timeout, TypeError: Expected string, received number ]
length : 2
invalidDate : Invalid Date
metadata : { endpoint: URL, headers: Headers, params: URLSearchParams }
nullConstructor : { constructor: null, name: "value" }
constructor : null
name : "value"
nullPrototype : { name: "value" }
name : "value"
numericStack : { stack: 42 }
stack : 42
tags : Set(4) { "typescript" , "angular" , "design-system" , … }
size : 4
typedArray : Uint8Array(2) [ 1, 2 ]
length : 2
byteLength : 2
byteOffset : 0
buffer : ArrayBuffer(2)
users : Map(3) { "alice" => Object , "bob" => Object , "charlie" => Object }
size : 3
<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:

PropTypeDefault
Icon used for the branch expand/collapse trigger.
| LucideIcon
| LucideIconData
| string
LucideChevronRight
Whether to render vertical indent guidelines for nested levels.
boolean
true
Type
| LucideIcon
| LucideIconData
| string
Description
Icon used for the branch expand/collapse trigger.
Type
boolean
Description
Whether to render vertical indent guidelines for nested levels.

Composite API

q-json-viewer-root

PropTypeDefault
The data to display in the JSON viewer. Accepts any JavaScript value.
unknown
The controlled checked node value
string[]
Truncates string values longer than this character count in the preview.
number
30
The initial checked node value when rendered. Use when you don't need to control the checked node value.
string[]
Expands branches up to this depth initially, without requiring explicit node IDs in defaultExpandedValue.

Ignored when
defaultExpandedValue is provided.
number
0
The initial expanded node ids when rendered. Use when you don't need to control the expanded node value.
string[]
The initial focused node value when rendered. Use when you don't need to control the focused node value.
string | null
The initial selected node value when rendered. Use when you don't need to control the selected node value.
string[]
The document's text/writing direction.
'ltr' | 'rtl'
The controlled expanded node ids
string[]
Whether clicking on a branch should open it or not
boolean
true
The value of the focused node
string | null
A root node to correctly resolve the Document in custom environments. i.e., Iframes, Electron.
() =>
| ShadowRoot
| Document
| Node
Groups array entries into chunks when the array length exceeds this value. Use a positive integer, or 0 to disable grouping. Invalid values default to 100.
number
100
HTML id attribute. If omitted, a unique identifier will be generated for accessibility.)
string
When true, the component will not be rendered in the DOM until it becomes visible or active.
boolean
false
Function to load children for a node asynchronously. When provided, branches will wait for this promise to resolve before expanding.
(details: {
indexPath: number[]
node: JsonNode
signal: AbortSignal
valuePath: string[]
}) => Promise<Array<JsonNode>>
Maximum number of items shown in a collapsed container preview.
number
3
Whether to wrap property keys in double quotes.
boolean
false
The controlled selected node value
string[]
Whether the tree supports multiple selection
  • 'single': only one node can be selected
  • 'multiple': multiple nodes can be selected
| 'single'
| 'multiple'
'single'
Callback function that determines whether a node should be hidden.
(
state: NodeState<JsonNode>,
) => boolean | undefined
Whether to show opening and closing brackets around expanded branches. Does not affect collapsed previews.
boolean
true
Whether to display non-enumerable properties (e.g. prototype methods).
boolean
true
The size of the tree and its elements. Governs properties like font size, item padding, and icon sizes.
'sm' | 'md'
'md'
Whether the tree supports typeahead search
boolean
true
When true, the component will be completely removed from the DOM when it becomes inactive or hidden, rather than just being hidden with CSS.
boolean
false
Called when the checked value changes
{
checkedNodes: Array<JsonNode>
checkedValue: string[]
}
Called when the tree is opened or closed
{
expandedNodes: Array<JsonNode>
expandedValue: string[]
focusedValue: string | null
}
Called when the focused node changes
{
focusedNode: JsonNode | null
focusedValue: string | null
}
Called when a node finishes loading children
{
collection: TreeCollection<JsonNode>
}
Called when loading children fails for one or more nodes
{
nodes: Array<{
error: Error
indexPath: number[]
node: JsonNode
valuePath: string[]
}>
}
Called when the selection changes
{
focusedValue: string | null
selectedNodes: Array<JsonNode>
selectedValue: string[]
}
Type
unknown
Description
The data to display in the JSON viewer. Accepts any JavaScript value.
Type
string[]
Description
The controlled checked node value
Type
number
Description
Truncates string values longer than this character count in the preview.
Type
string[]
Description
The initial checked node value when rendered. Use when you don't need to control the checked node value.
Type
number
Description
Expands branches up to this depth initially, without requiring explicit node IDs in defaultExpandedValue.

Ignored when
defaultExpandedValue is provided.
Type
string[]
Description
The initial expanded node ids when rendered. Use when you don't need to control the expanded node value.
Type
string | null
Description
The initial focused node value when rendered. Use when you don't need to control the focused node value.
Type
string[]
Description
The initial selected node value when rendered. Use when you don't need to control the selected node value.
Type
'ltr' | 'rtl'
Description
The document's text/writing direction.
Type
string[]
Description
The controlled expanded node ids
Type
boolean
Description
Whether clicking on a branch should open it or not
Type
string | null
Description
The value of the focused node
Type
() =>
| ShadowRoot
| Document
| Node
Description
A root node to correctly resolve the Document in custom environments. i.e., Iframes, Electron.
Type
number
Description
Groups array entries into chunks when the array length exceeds this value. Use a positive integer, or 0 to disable grouping. Invalid values default to 100.
Type
string
Description
HTML id attribute. If omitted, a unique identifier will be generated for accessibility.)
Type
boolean
Description
When true, the component will not be rendered in the DOM until it becomes visible or active.
Type
(details: {
indexPath: number[]
node: JsonNode
signal: AbortSignal
valuePath: string[]
}) => Promise<Array<JsonNode>>
Description
Function to load children for a node asynchronously. When provided, branches will wait for this promise to resolve before expanding.
Type
number
Description
Maximum number of items shown in a collapsed container preview.
Type
boolean
Description
Whether to wrap property keys in double quotes.
Type
string[]
Description
The controlled selected node value
Type
| 'single'
| 'multiple'
Description
Whether the tree supports multiple selection
  • 'single': only one node can be selected
  • 'multiple': multiple nodes can be selected
Type
(
state: NodeState<JsonNode>,
) => boolean | undefined
Description
Callback function that determines whether a node should be hidden.
Type
boolean
Description
Whether to show opening and closing brackets around expanded branches. Does not affect collapsed previews.
Type
boolean
Description
Whether to display non-enumerable properties (e.g. prototype methods).
Type
'sm' | 'md'
Description
The size of the tree and its elements. Governs properties like font size, item padding, and icon sizes.
Type
boolean
Description
Whether the tree supports typeahead search
Type
boolean
Description
When true, the component will be completely removed from the DOM when it becomes inactive or hidden, rather than just being hidden with CSS.
Type
{
checkedNodes: Array<JsonNode>
checkedValue: string[]
}
Description
Called when the checked value changes
Type
{
expandedNodes: Array<JsonNode>
expandedValue: string[]
focusedValue: string | null
}
Description
Called when the tree is opened or closed
Type
{
focusedNode: JsonNode | null
focusedValue: string | null
}
Description
Called when the focused node changes
Type
{
collection: TreeCollection<JsonNode>
}
Description
Called when a node finishes loading children
Type
{
nodes: Array<{
error: Error
indexPath: number[]
node: JsonNode
valuePath: string[]
}>
}
Description
Called when loading children fails for one or more nodes
Type
{
focusedValue: string | null
selectedNodes: Array<JsonNode>
selectedValue: string[]
}
Description
Called when the selection changes

q-json-viewer-tree

PropTypeDefault
Icon used for the branch expand/collapse trigger.
| LucideIcon
| LucideIconData
| string
LucideChevronRight
Whether to render vertical indent guidelines for nested levels.
boolean
true
Type
| LucideIcon
| LucideIconData
| string
Description
Icon used for the branch expand/collapse trigger.
Type
boolean
Description
Whether to render vertical indent guidelines for nested levels.
Updated by Dustin Pham