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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@toolsnap/json-beautify

v1.0.0

Published

Lightweight JSON formatting library — beautify, minify, and validate JSON with zero dependencies.

Downloads

75

Readme

@toolsnap/json-beautify

A lightweight, zero-dependency JSON formatting library for Node.js. Beautify, minify, and validate JSON with a clean, predictable API.

Installation

npm install @toolsnap/json-beautify

Why This Library?

Working with JSON in Node.js is routine, but the built-in JSON.stringify only goes so far. You constantly need to: pretty-print API responses for debugging, minify payloads before transmission, and validate user-provided JSON with meaningful error information. This package wraps those three operations into a simple, consistent interface.

For an interactive online tool with syntax highlighting and error indicators, try the JSON Formatter on Risetop — it supports tree view, path copying, and batch formatting.

API

beautify(input, indent = 2)

Pretty-prints JSON with configurable indentation. Accepts either a JSON string or a plain JavaScript object.

const { beautify } = require('@toolsnap/json-beautify');

// From string
beautify('{"name":"Alice","age":30}');
// {
//   "name": "Alice",
//   "age": 30
// }

// From object
beautify({ users: [{ id: 1, role: 'admin' }] }, 4);
// {
//     "users": [
//         {
//             "id": 1,
//             "role": "admin"
//         }
//     ]
// }

Throws if the input string contains invalid JSON.

minify(input)

Removes all unnecessary whitespace from a JSON string or object.

const { minify } = require('@toolsnap/json-beautify');

const original = '{\n  "name": "test",\n  "items": [1, 2, 3]\n}';
minify(original);
// '{"name":"test","items":[1,2,3]}'

console.log('Size saved:', original.length - minify(original).length, 'bytes');

validate(input)

Validates a JSON string and returns detailed diagnostics including error position.

const { validate } = require('@toolsnap/json-beautify');

validate('{"valid": true}');
// { valid: true, error: null, data: { valid: true }, position: null }

validate('{"missing": ');
// { valid: false, error: 'Unexpected end of JSON input', data: null, position: { position: 12 } }

The returned object always has four fields: valid, error, data, and position.

Real-World Examples

Logging API Responses

const { beautify } = require('@toolsnap/json-beautify');

// After fetching from an API
const response = await fetch('https://api.example.com/users');
const data = await response.json();

console.log(beautify(data, 2));

Processing Configuration Files

const { beautify, minify, validate } = require('@toolsnap/json-beautify');
const fs = require('fs');

// Read, validate, reformat
const raw = fs.readFileSync('config.json', 'utf8');
const result = validate(raw);

if (!result.valid) {
  console.error(`Invalid JSON at position ${JSON.stringify(result.position)}: ${result.error}`);
  process.exit(1);
}

fs.writeFileSync('config.min.json', minify(result.data));
fs.writeFileSync('config.pretty.json', beautify(result.data, 4));

HTTP Response Compression Helper

const { minify, validate } = require('@toolsnap/json-beautify');

function jsonMiddleware(req, res, next) {
  const originalSend = res.json.bind(res);
  res.json = (data) => {
    const body = JSON.stringify(data);
    if (validate(body).valid) {
      res.setHeader('Content-Type', 'application/json');
      res.send(minify(body));
    } else {
      res.status(500).send('Invalid JSON');
    }
  };
  next();
}

Browser Usage

This is a CommonJS module. Bundle with your preferred tool (webpack, esbuild, Rollup) for browser use.

Related Tools

License

MIT