JSON Formatter Guide: Beautify, Validate and Debug JSON

A practical guide to formatting and validating JSON: a worked example, what each error message means, the common syntax mistakes, and debugging workflows.

JSON Formatter Guide: Beautify, Validate and Debug JSON
Share this article:

An API response arrives as one 4,000-character line. A config file fails to load with "Unexpected token". A teammate pastes a payload into chat and asks what is wrong with it. All three are the same job: turn JSON into something you can read, check that it parses, and find the broken character when it does not.

This guide covers formatting, minifying and validating JSON with the JSON formatter, how to read parser error messages, the syntax mistakes behind most of them, and what formatting silently changes in your data.

What a JSON formatter does

JSON has six kinds of values: objects in braces, arrays in brackets, strings in double quotes, numbers, true/false, and null. Whitespace between them is meaningless to a parser, which is why APIs send it all on one line. A formatter parses the text and writes it back out in one of three ways:

  • Format indents nested values by two spaces, one key per line, so you can see the structure.
  • Minify removes every unnecessary space and line break, for sending or storing.
  • Validate only checks that the text parses, and reports the top-level type and number of keys.

Output updates about 300 ms after you stop typing, so you can fix an error and watch it clear.

Worked example: a minified order, formatted

Paste this response from an orders API:

{"id":"ord_1042","status":"paid","total":49.9,"customer":{"email":"ana@example.com","tier":"gold"},"items":[{"sku":"MUG-01","qty":2,"price":12.5},{"sku":"TEE-04","qty":1,"price":24.9}],"coupon":null}

Format gives:

{
  "id": "ord_1042",
  "status": "paid",
  "total": 49.9,
  "customer": {
    "email": "ana@example.com",
    "tier": "gold"
  },
  "items": [
    {
      "sku": "MUG-01",
      "qty": 2,
      "price": 12.5
    },
    {
      "sku": "TEE-04",
      "qty": 1,
      "price": 24.9
    }
  ],
  "coupon": null
}

Now it is obvious that items is an array of two objects, that coupon is present but null rather than missing, and that total is a number, not a string. Validate reports Type: object and Keys: 6.

The formatted version is 310 characters; the minified original is 199, about 36% smaller. Indentation is a large share of a small payload. On a real API, gzip or Brotli compression on the server removes most of that difference anyway, so minifying matters most for JSON stored in databases, URLs, cookies or local storage.

Reading JSON error messages

When parsing fails, a red bar shows the parser's message. The exact wording depends on your browser; these are Chrome's, with the position counted from 0 at the first character:

InputError (Chrome)Cause
{'name': 'Ada'}Expected property name or '}' in JSON at position 1Single quotes
{name: "Ada"}Expected property name or '}' in JSON at position 1Unquoted key
{"a": 1 "b": 2}Expected ',' or '}' after property value in JSON at position 8Missing comma
{"a": undefined}Unexpected token 'u' ... is not valid JSONJavaScript value, not JSON
{"a": 1} // noteUnexpected non-whitespace character after JSON at position 9Comment after the data

The position points at the character where the parser gave up, which is usually just after the real mistake. For a missing comma, look at the end of the previous value. Firefox and Safari word the same errors differently, and some versions add a line and column, but the position logic is the same.

The first two rows are by far the most common, usually from JSON written by hand or copied out of Python or JavaScript code. The full set of causes and fixes is in Expecting property name enclosed in double quotes.

The syntax mistakes behind most errors

  • Single quotes. JSON strings and keys must use double quotes. 'Ada' is valid JavaScript and invalid JSON.
  • Unquoted keys. {name: "Ada"} works in JavaScript and fails in JSON.
  • Trailing commas. [1, 2, 3,] is rejected by strict parsers. This formatter removes trailing commas before a closing brace or bracket before parsing, so pasted JavaScript object literals with a dangling comma still go through, and the output is clean, strict JSON.
  • Comments. JSON has no comments, so // and /* */ both fail. Config formats that allow comments, such as tsconfig.json, use a looser dialect.
  • Python values. True, False and None are Python; JSON needs true, false and null. Python dicts printed with print() also use single quotes. Convert them with the Python dict to JSON converter instead of fixing quotes by hand.
  • Raw line breaks inside strings. A string must write a newline as \n. A literal line break inside quotes gives "Bad control character in string literal".
  • NaN, Infinity and undefined. None of these exist in JSON. Send null or a string.

What formatting changes besides whitespace

Format and Minify parse your JSON into JavaScript values and serialize them again. Usually that is invisible, but a few things can differ from your input, and they matter if you paste the result back into a request:

  • Large integers lose precision. 12345678901234567890 comes back as 12345678901234567000, because JavaScript numbers hold about 15-17 significant digits. Send big IDs as strings.
  • Number formatting is normalized. 1.50 becomes 1.5 and 1e3 becomes 1000.
  • Duplicate keys collapse. In {"a":1,"a":2} only the last value survives.
  • Integer-like keys move first. {"b":1,"2":1,"10":1} comes out as {"2":1,"10":1,"b":1}.
  • Escapes are decoded. "Café" is output as "Café".

None of this changes what a well-behaved API means by the data, but it explains why a "formatted" payload is not always byte-for-byte equivalent.

Debugging workflows that save time

Two responses that should match. Formatting both and scrolling side by side misses things. Paste them into Compare JSON, which ignores key order and whitespace and lists each difference by path, such as items[0].qty, including type changes like 49.9 becoming "49.90".

One record per line. Logs and data exports often use JSON Lines, one object per line, which is not valid JSON as a whole. The JSONL viewer converts it to an array and reports bad lines by number. The difference is explained in JSONL vs JSON.

Typing the response in your code. Paste a formatted sample into JSON to TypeScript to get interfaces, with fields missing from some array items marked optional.

Deeply nested data for a spreadsheet. The JSON flattener turns nesting into dot-notation keys such as customer.email.

JSON inside a string. If a field contains escaped JSON like "{\"a\":1}", format the outer document, copy the inner string value without its surrounding quotes, unescape it, and format it separately.

Format and validate JSON →

Frequently asked questions

What is the difference between JSON and a JavaScript object?

JSON is a text format with strict rules: double-quoted keys and strings, no comments, no trailing commas, and no functions or undefined. A JavaScript object literal is code and allows all of those. Valid JSON is almost always a valid JavaScript literal; the reverse is often not true.

Should production APIs send formatted or minified JSON?

Minified, with HTTP compression turned on. Formatting is for people; once gzip or Brotli is applied the size difference is small, but there is no reason to send indentation.

Why does the error position not point at the mistake?

The parser reports where it got confused, which is often the character after the real problem. For a missing comma, check the end of the value before the reported position.

Is my JSON sent to a server?

No. Parsing and formatting run in your browser, so you can safely format responses that contain tokens or customer data. Even so, redact secrets before sharing a formatted payload with anyone.

Can this formatter fix single quotes or comments?

No. It only removes trailing commas automatically. Single quotes, unquoted keys and comments still fail, and the error message shows where.