Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
JSONFormatter
✓ import JSONFormatter from 'json-formatter-js';
✗ const JSONFormatter = require('json-formatter-js');
Default export; ESM-only package (no CJS). TypeScript types are included.
JSONFormatter
✓ const formatter = new JSONFormatter(json, open, config);
✗ new JSONFormatter(JSON.stringify(json));
Constructor expects a parsed object/array, not a JSON string.
JSONFormatter
✓ formatter.render() returns an HTMLElement node; appendChild() to document.
✗ formatter.render() returns a string; innerHTML = formatter.render();
render() returns a DOM node (not HTML string).
JSONFormatter
✓ formatter.openAtDepth(depth);
✗ formatter.openAtDepth({ depth: 3 });
openAtDepth expects a number, not an options object.
JSONFormatter
✓ new JSONFormatter(obj, 0);
✗ new JSONFormatter(obj, false);
Second argument 'open' is a number (default 1), not boolean.
Renders a JSON object with custom config and demonstrates collapsing all levels via openAtDepth().
import JSONFormatter from 'json-formatter-js';
const sampleJSON = {
name: 'JSON Formatter',
version: '2.5.23',
features: ['collapsible', 'themed', 'hover preview'],
nested: { enabled: true, count: 42 }
};
const formatter = new JSONFormatter(sampleJSON, 2, {
hoverPreviewEnabled: true,
animateOpen: true,
theme: 'dark'
});
const container = document.getElementById('json-view');
container.appendChild(formatter.render());
// Collapse all levels later
formatter.openAtDepth(0);
Errors
Common errors & fixes
Uncaught TypeError: formatter.render is not a function
Using require() to import the package (v2 is ESM-only).
fixSwitch to import JSONFormatter from 'json-formatter-js'; or use dynamic import().
TypeError: Cannot read properties of undefined (reading 'appendChild')
render() returns a DOM node, not a string; trying to set innerHTML.
fixUse document.getElementById('container').appendChild(formatter.render()); TypeError: argument is not an Object or Array
Passing a JSON string instead of a parsed object to the constructor.
fixParse the string first: new JSONFormatter(JSON.parse(jsonString));
TypeError: openAtDepth is not a function
Attempting to call openAtDepth before render() is called or after DOM removal.
fixEnsure render() has been called and formatter variable is still valid.
Audit
Dependencies
No dependency data recorded yet.