UUID Studio

JSON Formatter

Pretty-print JSON with live syntax colors.

  • 🔒 No data stored or uploaded
  • âš¡ 100% client-side
  • 🆓 Free, no account

Need more than one tool at a time? Open the full Workbench - or press Ctrl+K to jump to any tool.

Create or edit JSON here (syntax colors), then format, validate, or convert - same as MongoDB $binary UUID blobs on the Convert tab once detected.

New document

About JSON Formatter

Formatting JSON is really two jobs: making a minified payload readable, and telling you precisely why an invalid one will not parse. The second is what most people arrive needing, so this tool reports the position and the cause rather than just refusing the input.

JSON is a much smaller language than the JavaScript object syntax it resembles, and nearly every parse failure comes from that gap. Keys must be in double quotes, trailing commas are not allowed, single quotes are not allowed, comments are not allowed, and NaN, Infinity and undefined are not valid values. Anything that accepts those is parsing JSON5 or JSONC, not JSON.

Indentation itself carries no meaning - a formatted and a minified document are the same data - but two spaces is the near-universal convention and what most tooling and diff review expects. Minified output is what you want on the wire; formatted output is what you want in a file people read.

One thing to be careful about with any JSON tool: numbers. Most parsers turn JSON numbers into IEEE-754 doubles, so an integer larger than 2^53 loses its low digits on the way through. If your document contains snowflake ids or large monetary values in minor units, they should be strings.

Everything runs in your browser - a real API response with customer data in it never leaves the tab, which is exactly the situation where an online formatter usually gives you pause.

How to use the JSON Formatter

  1. Paste the JSON, however mangled - a minified blob, a log line, or something that does not parse at all.
  2. Read the verdict. If it is invalid, the error names the position, and the causes listed further down this page cover almost every case.
  3. Format for reading or minify for transport, and copy the result.
  4. For comparing two documents, sort the keys first so the diff shows real differences rather than reordering.

Examples

  • Minified input
    {"name":"app","active":true,"tags":["api","v2"]}

JSON Formatter in code

The same operation this tool performs, in the languages you are most likely to need it.

JavaScript / Node.js
// Pretty-print with 2-space indentation
const pretty = JSON.stringify(JSON.parse(raw), null, 2);

// Minify
const small = JSON.stringify(JSON.parse(raw));

// Sort keys recursively for a stable diff
const sortKeys = (v) =>
  Array.isArray(v) ? v.map(sortKeys)
  : v && typeof v === "object"
    ? Object.fromEntries(Object.keys(v).sort().map((k) => [k, sortKeys(v[k])]))
    : v;
console.log(JSON.stringify(sortKeys(JSON.parse(raw)), null, 2));

// Get a useful error rather than a bare throw
try { JSON.parse(raw); }
catch (e) { console.error(e.message); }  // includes the position
Python
import json

data = json.loads(raw)
print(json.dumps(data, indent=2, sort_keys=True, ensure_ascii=False))

# ensure_ascii=False keeps non-ASCII readable instead of \uXXXX escapes.

# Minify - the separators matter, the default leaves spaces
print(json.dumps(data, separators=(",", ":")))

# Precise error location
try:
    json.loads(raw)
except json.JSONDecodeError as e:
    print(f"{e.msg} at line {e.lineno} column {e.colno}")
Command line (jq / python)
# Pretty-print
jq . file.json

# Minify
jq -c . file.json

# Sort keys recursively - makes two files diffable
jq -S . file.json

# Validate only, exit non-zero on failure (useful in CI)
jq empty file.json

# No jq available
python3 -m json.tool file.json

When you need this

  • Making a minified API response readable so you can find the field you need.
  • Finding out exactly why a config file or webhook body will not parse.
  • Minifying a payload before pasting it into a test fixture or an environment variable.
  • Sorting keys so two versions of a document can be diffed meaningfully.
  • Checking whether a value is a JSON string containing JSON, which needs unescaping twice.

Common problems and what causes them

"Unexpected token < in JSON at position 0"
The response was HTML, not JSON - almost always an error page, a login redirect, or a proxy notice. Your code called response.json() on a 404 or 500 body. Check the status code and the content-type header before parsing.
"Unexpected end of JSON input"
The document is truncated: a stream cut short, a response body read twice, a log line clipped at a column limit, or a file that never finished writing. Count your closing braces and brackets - the tool points at where the structure stops making sense.
"Expected property name or '}' in JSON at position N"
Usually a trailing comma before a closing brace or bracket, which JavaScript object literals allow and JSON does not. It can also be an unquoted key: JSON requires double quotes around every property name.
Single quotes instead of double quotes
JSON only accepts double quotes, for both keys and string values. Single-quoted output is a sign the data came from a Python repr, a JavaScript object literal, or a log statement rather than a real JSON serialiser.
NaN, Infinity, or undefined in the document
None of these are valid JSON. They appear when a serialiser wrote out floating-point specials or a JavaScript undefined. Emit null instead, or a string if the distinction matters.
A byte-order mark at the start of the file
A UTF-8 BOM (EF BB BF) before the opening brace makes strict parsers report an unexpected character at position 0, even though the file looks perfect in an editor. Save as UTF-8 without BOM.
Comments in a JSON file
// and /* */ are not part of JSON, however common they are in config files. Tools that accept them are parsing JSON5 or JSONC. Strip comments before feeding the document to a strict parser.
Large integers losing precision
JSON numbers become IEEE-754 doubles in most parsers, so integers above 2^53 - Twitter-style snowflake ids, for instance - silently lose their last digits. Serialise those as strings, or use a parser with bigint support.

FAQ

Why does my JSON say 'Unexpected token < in JSON at position 0'?
Because the body is HTML, not JSON. Something returned an error page, a login redirect or a proxy notice, and your code called .json() on it anyway. Log the raw response and check the status code and content-type before parsing.
Is a trailing comma allowed in JSON?
No. JavaScript object and array literals permit it, JSON does not - which is why a payload pasted from source code so often fails to parse. Remove it, or use JSON5 if you control both ends.
Can JSON contain comments?
Not per the specification. Formats like JSONC and JSON5 add them, and tools such as VS Code accept them in config files, but a strict parser will reject them. Strip comments before parsing.
Does formatting change my data?
No - whitespace between tokens is not significant, so a formatted and a minified document parse to identical values. Key order is also not significant to the specification, though it is preserved by most parsers, which is why sorting keys is useful before diffing.
Why do my large numbers change?
JSON numbers are read as IEEE-754 doubles by most parsers, which represent integers exactly only up to 2^53. Beyond that the low digits are lost. Serialise large ids as strings, or use a bigint-aware parser.
Is my JSON uploaded anywhere?
No. Parsing and formatting happen in your browser, with no request - which is what makes it safe to paste a real response containing customer data. The page also works offline once loaded.
Does it fix trailing commas?
No - invalid JSON is reported with a parse error location so you can fix the source.

Related reading