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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@kikomi/json-formatter

v2.0.1

Published

JSON Formatter

Downloads

8

Readme

JSON Formatter

Formats a JSON-string into a properly indented and aligned structure. The package contains two implementations of the formatter:

  • JSONFormatter - formats a JSON-string into an indented JSON represented in HTML
  • JSONFormatterBasic - formats a JSON-string into an indented JSON represented in string

The indentation (number of spaces) is controlled via indentation parameter. The formatter operates only with strings without having to converting it into a JSON object allowing it to format non-strictly valid JSON string i.e. a JSON represented by an array of objects or a JSON that has non-quoted properties.

Example: The input string

{"_id":"5f0227d65344c4288a30e9e2","age":35,"name": "Lucia Porter","gender":"female","email":"[email protected]","phone":"+1 (876) 517-2377","address":"717 Guernsey Street, Savannah, Guam, 6990","about": "Mollit in anim ex \"Lorem Lorem\" reprehenderit id 'dolore' irure qui.","tags":["amet","ullamco",45,"id","Lorem"]}

gets formatted:

  • Using JSONFormatter()
<div class="jf-bracket-curly jf-bracket-open">{</div>
<div class="jf-section" style="margin-left: 24px;">
    <div><span class="jf-property">"_id"</span><span class="jf-colon">:</span>&nbsp;<span class="jf-property">"5f0227d65344c4288a30e9e2"</span><span class="jf-comma">,</span></div>
    <div><span class="jf-property">"age"</span><span class="jf-colon">:</span>&nbsp;35<span class="jf-comma">,</span></div>
    <div><span class="jf-property">"name"</span><span class="jf-colon">:</span>&nbsp;<span class="jf-property">"Lucia Porter"</span><span class="jf-comma">,</span></div>
    <div><span class="jf-property">"gender"</span><span class="jf-colon">:</span>&nbsp;<span class="jf-property">"female"</span><span class="jf-comma">,</span></div>
    <div><span class="jf-property">"email"</span><span class="jf-colon">:</span>&nbsp;<span class="jf-property">"[email protected]"</span><span class="jf-comma">,</span></div>
    <div><span class="jf-property">"phone"</span><span class="jf-colon">:</span>&nbsp;<span class="jf-property">"+1 (876) 517-2377"</span><span class="jf-comma">,</span></div>
    <div><span class="jf-property">"address"</span><span class="jf-colon">:</span>&nbsp;<span class="jf-property">"717 Guernsey Street, Savannah, Guam, 6990"</span><span class="jf-comma">,</span></div>
    <div><span class="jf-property">"about"</span><span class="jf-colon">:</span>&nbsp;<span class="jf-property">"Mollit in anim ex \"Lorem Lorem\" reprehenderit id \'dolore\' irure qui."</span><span class="jf-comma">,</span></div>
    <div>
        <span class="jf-property">"tags"</span><span class="jf-colon">:</span>&nbsp;<span class="jf-bracket-square jf-bracket-open">[</span>
        <div class="jf-section" style="margin-left: 24px;">
            <div><span class="jf-property">"amet"</span><span class="jf-comma">,</span></div>
            <div><span class="jf-property">"ullamco"</span><span class="jf-comma">,</span></div>
            <div>45<span class="jf-comma">,</span></div>
            <div><span class="jf-property">"id"</span><span class="jf-comma">,</span></div>
            <div><span class="jf-property">"Lorem"</span></div>
        </div>
        <div class="jf-bracket-square jf-bracket-close">]</div>
    </div>
</div>
<div class="jf-bracket-curly jf-bracket-close">}</div>

You can customize the way {,},[,],,,: and JSON properties look using predefined CSS-classes. See the demo project for details.

  • Using JSONFormatterBasic()
{
    "_id": "5f0227d65344c4288a30e9e2",
    "age": 35,
    "name": "Lucia Porter",
    "gender": "female",
    "email": "[email protected]",
    "phone": "+1 (876) 517-2377",
    "address": "717 Guernsey Street, Savannah, Guam, 6990",
    "about": "Mollit in anim ex \"Lorem Lorem\" reprehenderit id 'dolore' irure qui.",
    "tags": [
        "amet",
        "ullamco",
        45,
        "id",
        "Lorem"
    ]
}

Note: If an input string doesn't represent a valid JSON, the original input string gets returned.

Invalid JSON input string:

{"_id":"5f0227d65344c42","age":35,name": "Lucia Porter"}

the output:

{"_id":"5f0227d65344c42","age":35,name": "Lucia Porter"}

Usage

  1. Import the @kikomi/json-formatter package into you project run:
npm i @kikomi/json-formatter --save
  1. Create an instance of JSONFormatter in your code:
// with default indentation
var jsonFormatter = new JSONFormatter();

// with preset indentation
var jsonFormatter = new JSONFormatter(16);
  1. Format your JSON input:
var input = '{"your":"json","string":999}';
var output = jsonFormatter.format(input);

the package comes with Typescript type definitions.

Development

  1. Install NodeJS
  2. Install Gulp
npm i -g gulp
  1. Install dependencies
npm i

Running tests

npm run test

or run tests in debug mode

npm run test:debug

Running demo project

  1. Navigate to the demo folder
  2. Install dependent packages
npm i
  1. Build and run the demo project:
npm run serve
  1. Navigate to localhost:8080

Building the project

npm run build