Need help with your JSON?

Try our JSON Formatter tool to automatically identify and fix syntax errors in your JSON. JSON Formatter tool

Using JSON Formatter NPM Packages in JavaScript Projects

If you are searching for a JSON formatter npm package, the first question is what you actually need to format: a JavaScript object in memory, a raw JSON string, checked-in .json files, or package.json. Those are related jobs, but the best tool is different for each one.

In most JavaScript projects, the right answer is one of four options: built-in JSON.stringify() for simple pretty-printing, prettier for formatting JSON text and files, json-stable-stringify for deterministic key order, and specialized formatters like prettier-package-json when you want package-specific ordering rules.

Start with JSON.stringify()

If you already have a JavaScript object and only need readable output, you usually do not need an extra npm package. The native JSON.stringify(value, replacer, space) API is still the fastest path.

Pretty-print an object you already have in memory

const payload = {
  name: "OfflineTools",
  enabled: true,
  features: ["format", "validate", "diff"],
  settings: { indent: 2, sortKeys: false }
};

const pretty = JSON.stringify(payload, null, 2);

console.log(pretty);
/*
{
  "name": "OfflineTools",
  "enabled": true,
  "features": [
    "format",
    "validate",
    "diff"
  ],
  "settings": {
    "indent": 2,
    "sortKeys": false
  }
}
*/

This is ideal for logging, downloads, test fixtures, and quick debugging. What it does not do is sort keys, enforce formatting across files, or rescue invalid JSON text before parsing.

The Best JSON Formatter NPM Packages for Common Jobs

For most searchers, the practical short list looks like this:

  • Use prettier when you want to format JSON strings, JSON files, or JSONC config files in editors, scripts, and CI.
  • Use json-stable-stringify when your app or test suite needs deterministic key ordering from a JavaScript object.
  • Use prettier-plugin-sort-json when you want checked-in JSON files sorted by key under Prettier.
  • Use prettier-package-json when the target file is specifically package.json.

1. prettier for JSON strings and files

For most real projects, prettier is the default JSON formatter package to install. It handles editor integration, command-line formatting, and programmatic formatting from Node scripts. Current Prettier releases expose an async API, so use await prettier.format(...) rather than older synchronous examples.

Install and format JSON with Prettier

npm install --save-dev prettier

// format-json.mjs
import * as prettier from "prettier";

const rawJson = '{"z":1,"a":{"second":2,"first":1}}';

const formatted = await prettier.format(rawJson, {
  parser: "json"
});

console.log(formatted);
/*
{
  "z": 1,
  "a": {
    "second": 2,
    "first": 1
  }
}
*/

Prettier is especially useful when the source is text rather than an in-memory object. If you are formatting a real file, pass filepath so Prettier can infer the parser from the filename.

Use JSONC for config files with comments or trailing commas

import * as prettier from "prettier";

const tsconfigLikeJson = `{
  // comments are valid in JSONC
  "compilerOptions": {
    "strict": true,
  },
}`;

const formatted = await prettier.format(tsconfigLikeJson, {
  parser: "jsonc"
});

console.log(formatted);

That json versus jsonc distinction matters. A strict JSON formatter should reject comments and trailing commas; many config files that look like JSON are actually JSONC.

2. json-stable-stringify for deterministic output

If your goal is stable key ordering rather than just indentation, json-stable-stringify is the classic package to reach for. It is useful for snapshot tests, content hashing, cache keys, and places where two equivalent objects should always serialize to the same string.

Stable JSON with sorted keys

npm install json-stable-stringify

const stringify = require("json-stable-stringify");

const payload = {
  z: 1,
  a: 2,
  nested: {
    second: 2,
    first: 1
  }
};

const stableJson = stringify(payload, { space: 2 });

console.log(stableJson);
/*
{
  "a": 2,
  "nested": {
    "first": 1,
    "second": 2
  },
  "z": 1
}
*/

This solves a different problem than Prettier. Prettier formats text; json-stable-stringify creates deterministic JSON from JavaScript values.

3. prettier-plugin-sort-json for alphabetized JSON files

If your team wants checked-in JSON files sorted by key, a Prettier plugin is usually cleaner than writing a one-off script. The current published prettier-plugin-sort-json release is for Prettier 3 and declares Node 18+.

Sort JSON files during normal Prettier runs

npm install --save-dev prettier prettier-plugin-sort-json

// .prettierrc.json
{
  "plugins": ["prettier-plugin-sort-json"],
  "jsonRecursiveSort": true
}

// then run
npx prettier --write data.json

One important caveat: this plugin only affects files Prettier parses as regular json. It does not sort package.json, package-lock.json, or composer.json, because Prettier uses a different parser for those files.

4. prettier-package-json for package.json

For package.json, alphabetical sorting is often the wrong rule. prettier-package-json applies package-specific ordering instead, keeping important fields like name, version, scripts, and metadata in predictable positions.

Format package.json with a package-aware formatter

npm install --save-dev prettier-package-json

npx prettier-package-json --write package.json

Which Package Should You Install?

  • If you already have a JavaScript object and just want readable output, use JSON.stringify(obj, null, 2).
  • If you want a general JSON formatter npm package for files, editors, scripts, and CI, install prettier.
  • If you need deterministic serialization for tests, hashing, or cache keys, install json-stable-stringify.
  • If you want repository JSON files sorted alphabetically by key, add prettier-plugin-sort-json on top of Prettier.
  • If the file is package.json, prefer prettier-package-json.

Common Mistakes and Caveats

  • Formatting does not repair broken JSON. If the input is invalid, parse errors still need to be handled first.
  • JSON and JSONC are not the same. Config files like tsconfig.json often need a JSONC-aware formatter.
  • Sorted keys are not always desirable. Some APIs, fixtures, or hand-maintained config files are easier to read when order stays intentional rather than alphabetical.
  • Validation is a separate concern. If you need schema validation, use a validator such as ajv; formatters only control layout and ordering.
  • Do not add a runtime dependency if a dev tool is enough. For file formatting in CI or an editor, Prettier should usually stay in devDependencies.

Conclusion

The best JSON formatter npm package depends on where formatting happens. Use built-in JSON.stringify() for simple object output, prettier for general JSON file and text formatting, json-stable-stringify for deterministic serialization, and package-specific tools when file conventions matter. That gives search users a practical answer instead of a long list of vaguely related JSON utilities.

Need help with your JSON?

Try our JSON Formatter tool to automatically identify and fix syntax errors in your JSON. JSON Formatter tool