DevToolbox

How to Format JSON: Complete Guide with Examples

2026-05-23

Quick Answer

Quick Answer: The fastest way to format JSON is to paste it into a JSON Formatter that validates and pretty-prints in one step. In code, use JSON.stringify(obj, null, 2) in JavaScript. Details below.

What is JSON formatting?

JSON formatting (pretty-printing) adds indentation and line breaks so nested objects are readable. Minifying does the opposite—removes whitespace for smaller payloads.

Why format JSON?

  • Debugging: Logs and API responses are easier to scan when indented.
  • Code review: Diffs on formatted JSON highlight real data changes.
  • Documentation: Examples in README files should be readable.

Three ways to format JSON

1. Online formatter (fastest for ad-hoc work)

Use our JSON Formatter: paste, click Format, copy the result. Nothing leaves your browser.

2. JavaScript

const obj = { name: "DevToolbox", active: true };
console.log(JSON.stringify(obj, null, 2));

3. CLI with jq

echo '{"a":1}' | jq .

Common errors and fixes

ErrorFix
Trailing commaRemove comma after last property
Single quotesJSON requires double quotes for strings
CommentsStrip // lines—JSON has no comments

Pro tip: If JSON.parse fails near position 4000, your file may be one long line—format first, then search for the error line.

Try It Yourself

Ready to format your JSON? Try our free JSON Formatter

Frequently Asked Questions

How do I pretty print JSON in JavaScript?

Use JSON.stringify(value, null, 2) for 2-space indentation, or 4 for wider indent.

Can JSON have comments?

Standard JSON does not support comments. Use JSON5 in tools that explicitly support it, or remove comments before parsing.

Is minified JSON valid?

Yes. Minification only removes optional whitespace; data stays the same.

Try it yourself

Use our free JSON Formatter — no signup required.

Open JSON Formatter