formatJson
Pretty-print JSON. Accepts an object, or a JSON string to reformat.
API
formatJson(value, onError?, indent?)
| Parameter | Description | Type | Default |
|---|---|---|---|
value |
Object, or a JSON string (single quotes are tolerated) | string | object |
Required |
onError |
Called with the parse/serialize error | (e: Error) => void |
no-op |
indent |
Spaces per level | number |
4 |
Returns the formatted string, or '' when the input cannot be parsed.
Example
import { formatJson } from 'ranuts';
formatJson({ a: 1, b: [2, 3] });
formatJson("{'a': 1}"); // single quotes tolerated
formatJson({ a: 1 }, undefined, 2); // 2-space indent
formatJson('nope', (e) => console.warn(e)); // '' and the error handed to the callbackNotes
- A string input is re-parsed, not echoed. That validates it and normalizes the layout rather than trusting whatever spacing the caller had.
- Errors are reported, never thrown. Invalid JSON, circular structures and values
JSON.stringifycannot represent all return''and invokeonError.