npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-formatter-js

v2.5.18

Published

JSON Formatter core library

Downloads

182,706

Readme

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
  • hoverPreviewEnabled: enable preview on hover.
  • hoverPreviewArrayCount: number of array items to show in preview. Any array larger than this number will be shown as Array[XXX] where XXX is the length of the array.
  • hoverPreviewFieldCount: number of object properties to show for object preview. Any object with more properties than this number will be truncated.
Theme
  • theme: a string that can be any of these options: ['dark']. Look at src/style.less for making new themes.
Animation
  • animateOpen: enable animation when expanding a JSON object. True by default.
  • animateClose: enable animation when closing a JSON object. True by default.
Rendering Options
  • useToJSON: use the toJSON method to render an object as a string, if available. Useful for objects like Date or Mongo's ObjectID that might make more sense as a string than as empty objects. True by default.
  • sortPropertiesBy: use the given sorting function to deeply sort the object properties.
  • maxArrayItems: use to split arrays into multiple smaller groups. This value defines the size of each group. If the length of the array is less than this number, no groups are created. It's the same behavior as the Webkit developer tool's console.
  • exposePath: add an array of keys to each node dataset so it is possible to correlate nodes to the original JSON.

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