How to Format JSON in JavaScript (2026 Guide)

·8 min read

Unformatted JSON is one of the most common time-wasters in day-to-day development. A payload arrives on a single 4,000-character line, an error message points at "column 3,182", and you spend five minutes just finding the field you care about. This guide walks through every practical way to format JSON in JavaScript — in Node.js, in the browser, and from the command line — with the trade-offs of each.

The one-liner: JSON.stringify with indent

The built-in JSON.stringify takes an optional third argument that controls indentation. Pass 2 for two-space indent (the community default) or "\t" for tabs:

const raw = '{"name":"Ada","skills":["math","logic"],"active":true}';
const pretty = JSON.stringify(JSON.parse(raw), null, 2);
console.log(pretty);
// {
//   "name": "Ada",
//   "skills": [
//     "math",
//     "logic"
//   ],
//   "active": true
// }

The second argument is a replacer — pass null when you just want everything. This is the fastest, dependency-free option and works identically in Node.js and every modern browser.

Formatting JSON that might be invalid

If the input might be malformed, wrap the parse in a try/catch and surface the position of the error. V8 (Node and Chrome) puts a helpful position hint in the error message from ES2024 onward:

function formatJson(input) {
  try {
    return { ok: true, value: JSON.stringify(JSON.parse(input), null, 2) };
  } catch (err) {
    return { ok: false, error: err.message };
  }
}

For interactive tools, showing the exact line and column is far more useful than a raw "Unexpected token" message. That's what our free JSON Formatter does — it highlights the offending character in-place.

Minifying JSON

Formatting and minifying are two sides of the same call. Drop the indent argument and you get the tightest possible output:

const min = JSON.stringify(JSON.parse(raw));
// {"name":"Ada","skills":["math","logic"],"active":true}

Minified JSON is what you should send over the wire; formatted JSON is what you should read in a debugger.

Formatting from the command line

If you already have Node installed, you don't need a separate CLI. Pipe any JSON through this one-liner:

curl -s https://api.example.com/user | node -e "process.stdin.on('data', d => console.log(JSON.stringify(JSON.parse(d), null, 2)))"

Or use jq if you have it: curl -s ... | jq .. Both are fine — the point is not to eyeball raw single-line JSON.

Should you use a library?

For 95% of cases: no. JSON.stringify is enough. Reach for a library only when you need one of these:

  • Comments and trailing commas — use json5 or jsonc-parser.
  • Very large payloads (100MB+) — use a streaming parser like stream-json to avoid loading the whole tree into memory.
  • Sorted keys for deterministic output — use json-stable-stringify.

Common formatting mistakes

  • Double-encoding. If you call JSON.stringify on a value that's already a JSON string, you get a string wrapped in extra quotes and escaped backslashes. Always parse first, stringify second.
  • Losing precision on big numbers. JavaScript numbers are 64-bit floats; anything above 2^53 loses precision. If your API returns 64-bit IDs, format them as strings before you touch them.
  • Serializing dates as ISO strings by default. That's usually what you want, but if you need Unix timestamps, use a replacer function.

The fastest way to format JSON right now

If you just have a blob of JSON in your clipboard and want it readable in one second, paste it into our JSON Formatter. It's free, runs entirely in your browser, and highlights syntax errors with line and column numbers. No signup, no upload, nothing leaves your machine.

Keep reading