How to Format JSON Online (and Fix Common Errors)
3 min read · Published
Table of contents
Raw API responses and log lines often arrive as one long, unreadable line of JSON. Formatting (also called beautifying or pretty-printing) adds indentation and line breaks so you can see the structure. Minifying does the opposite for transport. This guide shows both, and how to fix the errors you will hit along the way.
Format JSON in four steps
- Open the JSON Formatter.
- Paste your JSON into the input panel.
- Choose an indentation width: 2 spaces, 4 spaces or tabs.
- Press Format / Beautify, then copy or download the result.
Before and after:
{"id":42,"user":{"name":"Ada","roles":["admin","dev"]},"active":true}
{
"id": 42,
"user": {
"name": "Ada",
"roles": [
"admin",
"dev"
]
},
"active": true
}
The two documents are identical in meaning, because whitespace between tokens is ignored by every JSON parser.
When to minify
Minified JSON is smaller, which matters for network payloads, URLs, environment variables and log lines. Press Minify in the formatter to strip all optional whitespace. If you serve JSON over HTTP, also enable gzip or Brotli compression; it reduces indentation overhead almost to zero, so keep readable JSON in source control and minify only at the boundary.
The five errors you will see most
1. Trailing comma
{ "a": 1, "b": 2, }
Remove the comma after the last item. This is the single most common mistake when editing JSON by hand.
2. Single quotes
{ 'name': 'Ada' }
JSON requires double quotes on both keys and strings.
3. Unquoted keys
{ name: "Ada" }
This is a JavaScript object literal. Wrap the key in double quotes.
4. Comments
{ "debug": true // temporary
}
Standard JSON has no comments. Delete them, or move to a format that supports them (JSONC, JSON5, YAML) for configuration files.
5. Unescaped characters in strings
{ "path": "C:\new\folder" }
Backslashes must be escaped: "C:\\new\\folder". Raw line breaks inside a string are also invalid; use \n.
Reading the error message
Browsers report errors like "Unexpected token } in JSON at position 17". Position counts characters from the start, which is hard to use on long text. The JSON Validator converts it to a line and column and prints the offending line with a caret underneath so you can see exactly where the parser gave up. Note that the error points to where parsing failed, which can be one token after the real mistake; a missing comma is usually reported at the start of the next key.
Formatting in your own tools
# Command line (Python is installed on most systems)
echo '{"a":1}' | python3 -m json.tool
# jq, if installed
cat response.json | jq .
// Node.js or browser console
console.log(JSON.stringify(JSON.parse(text), null, 2));
Privacy when using online formatters
JSON often contains tokens, emails or customer data. Only paste data into tools that process it locally. DeveloperHub's formatter runs in your browser with JSON.parse and JSON.stringify and never uploads your input. Even so, prefer redacted samples for anything sensitive.
Next steps
- Convert arrays of objects to spreadsheets with JSON to CSV.
- Learn the basics in What Is JSON and How Does It Work?.
- Understand the APIs that produce this JSON in REST API Basics.
Related tools
JSON Formatter
Format, beautify and minify JSON with custom indentation.
JSON Validator
Check JSON syntax and see the exact error position.
JSON to XML Converter
Convert JSON objects and arrays into well-formed XML.
FAQ
Does formatting JSON change its meaning?
No. Whitespace between tokens is insignificant in JSON, so formatted and minified versions represent identical data.
Is it safe to paste private JSON into an online formatter?
Only if the tool processes data locally. DeveloperHub runs entirely in your browser; still, avoid pasting production secrets into any website when a sample will do.
Why does my JSON fail with "Unexpected token"?
The parser found a character that is not allowed at that position, usually a trailing comma, single quotes, an unquoted key or a comment.
Keep reading
- What Is JSON and How Does It Work?
Learn what JSON is, its syntax rules, data types, common mistakes and how to read and write it in JavaScript and Python.
- REST API Basics
Understand REST APIs: resources, HTTP methods, status codes, JSON payloads, idempotency, pagination, versioning and authentication basics.