JSON Viewers

JSON Viewers

<div class="json-viewer" id="demo-json-viewer">
<textarea class="textarea" readonly aria-label="Raw post params">{"transaction": {"amount": 12900, "currency_code": "USD", "email": "emma.wilson@example.com", "billing_address": {"name": "Emma Wilson", "address1": "48 Harbor Street", "address2": "", "city": "Portland", "state": "ME", "zip": "04101", "country": "US", "phone": null}, "retain_on_success": true, "gateway_specific_fields": {"shopper_reference": "CUS_100482", "splits": [{"type": "BalanceAccount", "amount": {"currency": "USD", "value": 12513}}, {"type": "Commission", "amount": {"currency": "USD", "value": 387}, "description": "Fixed Fee"}]}}}</textarea>
</div>

The markup is a textarea holding the JSON

The page writes the box and the textarea. The script adds the buttons and draws the tree.

<div class="json-viewer">
<textarea class="textarea" readonly>{"amount": 12900, "currency_code": "USD"}</textarea>
</div>

data-collapsed starts the tree folded to its top level

<div class="json-viewer" id="demo-json-viewer-collapsed" data-collapsed>
<textarea class="textarea" readonly aria-label="Order">{"number": "110963", "status": "open", "total": "259.96", "lines": [{"sku": "LAMP-BLK", "quantity": 2}, {"sku": "SHEET-TWIN", "quantity": 1}], "shipping": {"method": "Standard", "tracking": null}}</textarea>
</div>

data-view="raw" starts on the textarea

<div class="json-viewer" id="demo-json-viewer-raw" data-view="raw">
<textarea class="textarea" readonly aria-label="Order">{"number": "110963", "status": "open", "total": "259.96", "lines": [{"sku": "LAMP-BLK", "quantity": 2}, {"sku": "SHEET-TWIN", "quantity": 1}], "shipping": {"method": "Standard", "tracking": null}}</textarea>
</div>

Without readonly the raw view can be edited, and text that is not JSON is marked invalid

Show parsed draws the tree from what is typed. The form sends the textarea.

<div class="field">
<label for="demo-json-viewer-edit-input">Metadata</label>
<div class="json-viewer" id="demo-json-viewer-edit" data-view="raw">
<textarea class="textarea" id="demo-json-viewer-edit-input" name="metadata">{
"gift_wrap": true,
"note": "Leave at the side door",
"priority": 2
}</textarea>
</div>
<p>Show parsed draws the tree from what is typed. The form sends the textarea.</p>
</div>

The page folds the tree with collapse() and expand(), and replaces the JSON through value

<div class="flex flex-col gap-3">
<div class="flex flex-wrap gap-2">
<button type="button" class="btn" data-variant="secondary" data-size="sm" onclick="document.getElementById('demo-json-viewer-script').collapse()">collapse()</button>
<button type="button" class="btn" data-variant="secondary" data-size="sm" onclick="document.getElementById('demo-json-viewer-script').expand()">expand()</button>
<button type="button" class="btn" data-variant="secondary" data-size="sm" onclick="document.getElementById('demo-json-viewer-script').value = { refunded: true, amount: 4500, lines: ['LAMP-BLK'] }">Set value</button>
</div>
<div class="json-viewer" id="demo-json-viewer-script">
<textarea class="textarea" readonly aria-label="Order">{"number": "110963", "status": "open", "total": "259.96", "lines": [{"sku": "LAMP-BLK", "quantity": 2}, {"sku": "SHEET-TWIN", "quantity": 1}], "shipping": {"method": "Standard", "tracking": null}}</textarea>
</div>
</div>

API Reference

Attributes

Attribute On Values Default Description
data-view .json-viewer "parsed", "raw" "parsed" "raw" starts on the textarea. The script keeps it up to date as the view changes.
data-collapsed .json-viewer present or absent absent The tree starts folded to its top level. The outermost object or list is open.
readonly the textarea present or absent absent The JSON cannot be edited in the raw view. Without it the textarea is an ordinary field.
name the textarea text none Sends the JSON with the form.
aria-invalid the textarea "true" set by the script Set when Show parsed finds text that is not JSON; the viewer stays on the textarea. Removed on the next keystroke.
open a details in the tree present or absent set by the script Present while that object or list is unfolded.

Methods and properties

Member Type Description
expand() method Unfolds every object and list.
collapse() method Folds every object and list inside the outermost one, which stays open so its keys can be read.
view property Read: "parsed" or "raw". Set: "raw" shows the textarea, anything else draws the tree.
value property Read: the textarea's text. Set: takes JSON text, or an object or list, and draws it.

Keyboard

Key Description
Tab Through the buttons, then through each object and list in the tree.
Enter or Space On an object or a list: folds or unfolds it. This is the browser's own behaviour for details.

The JSON viewer sends no events of its own. The textarea sends its own input and change events when it is edited.

Show raw indents the JSON by two spaces. Text that is not JSON is left as typed.

Keys use the text colour and strings the success colour. Numbers and booleans use the info colour. null uses the destructive colour.

Once the script has started it, the element carries data-json-viewer-initialized="true" and sends tsugi:initialized. The methods and properties above are on the element itself.