Home

Awesome

JSON Formatter

Render JSON objects in HTML with a collapsible navigation.

Check out <pretty-json> Custom Element for a more portable and light solution

Usage

Live Demo

Install via npm

npm install --save json-formatter-js

Include json-formatter.js from the dist folder in your page.

import JSONFormatter from "json-formatter-js";

const myJSON = { ans: 42 };

const formatter = new JSONFormatter(myJSON);

document.body.appendChild(formatter.render());

API

JSONFormatter(json [, open [, config] ])

json (Object) - required

The JSON object you want to render. It has to be an object or array. Do NOT pass a raw JSON string.

open (Number)

Default: 1 This number indicates up to how many levels the rendered tree should expand. Set it to 0 to make the whole tree collapsed or set it to Infinity to expand the tree deeply.

config (Object)

Default:

{
  hoverPreviewEnabled: false,
  hoverPreviewArrayCount: 100,
  hoverPreviewFieldCount: 5,
  animateOpen: true,
  animateClose: true,
  theme: null,
  useToJSON: true,
  sortPropertiesBy: null,
  maxArrayItems: 100,
  exposePath: false
}

Available configurations:

Hover Preview
Theme
Animation
Rendering Options

openAtDepth([depth])

const formatter = new JSONFormatter({ ... });
document.body.appendChild(formatter.render());
formatter.openAtDepth(3);

depth (Number)

Default: 1 This number indicates up to how many levels the rendered tree should open. It allows use cases such as collapsing all levels (with value 0) or expanding all levels (with value Infinity).

Development

Install the dependencies:

npm install

Run the dev server:

npm run dev

Running Tests

Once:

npm test

License

MIT