JSON Viewer
A highlighted, foldable JSON tree with a copy button.
JsonViewer is new in v0.4.0. It renders JSON as a tree with highlighted keys, strings, numbers and literals. Objects and arrays fold with a click, and a copy button copies the whole document, indented.
Installation
shellui add json-viewer
This also installs json-viewer-node (one level of the tree) and copy-button.
Usage
From an object
Value takes any object and serializes it with System.Text.Json. Two levels are open at first.
<JsonViewer Value="sample" Class="w-full max-w-xl max-h-80" />
@code {
private readonly object sample = new
{
name = "ShellUI",
version = "0.4.0",
stable = true,
components = 98,
targets = new[] { "net8.0", "net9.0", "net10.0" },
theme = new { radius = "0.5rem", colors = new { primary = "oklch(0.21 0.006 285.88)", destructive = "oklch(0.58 0.24 28.48)" } },
homepage = (string?)null
};
}
From a string
Json takes a JSON string and wins over Value. ExpandDepth sets how many levels start open, and ShowCopy="false" hides the copy button. Text that isn't valid JSON shows the parser's error instead of a tree.
Invalid JSON: Expected start of a property name or value, but instead reached end of data. LineNumber: 0 | BytePositionInLine: 21.
<div class="grid w-full max-w-xl gap-3">
<JsonViewer Json="@json" ExpandDepth="1" ShowCopy="false" />
<JsonViewer Json="@("{ \"name\": \"ShellUI\", ")" />
</div>
@code {
private const string json = """
{
"id": "toast_42",
"variant": "success",
"message": "Saved",
"meta": { "durationMs": 4000, "dismissible": true },
"actions": [ { "label": "Undo" } ]
}
""";
}
When the JSON changes, the tree is rebuilt and the open levels reset to ExpandDepth.
API Reference
| Parameter | Type | Default | Description |
|---|---|---|---|
Json |
string? |
null |
The JSON to show |
Value |
object? |
null |
An object to serialize and show when Json isn't set |
ExpandDepth |
int |
2 |
Levels open on first render; deeper objects and arrays start folded |
ShowCopy |
bool |
true |
Shows a copy button in the top-right corner |
Class |
string? |
null |
Additional CSS classes; add a max-h-* class to scroll long documents |
Accessibility
- Folding toggles are buttons, so they can be reached with Tab
- The copy button is a Copy Button and confirms with a check mark