← All guides

When to beautify, minify, or validate JSON

The syntax errors that trip people up most often, the large-number precision trap, and which operation fits which situation.

json beautify json formatter json minify json validate

Three operations with different purposes

Beautify exists for humans. Line breaks and indentation follow the nesting so the shape of the data becomes visible.

Minify exists for machines. Stripping whitespace reduces payload size, which is why most API responses arrive as a single line.

Validate confirms the syntax. In practice both other operations must parse the input first, so a successful conversion is itself proof that the document is valid JSON.

The most common syntax errors

JSON resembles a JavaScript object literal but is considerably stricter. These mistakes recur constantly.

  • Trailing comma: {"a":1,} is fine in JavaScript and invalid in JSON, and the same applies at the end of an array.
  • Single quotes: JSON permits double quotes only, for both keys and string values.
  • Unquoted keys: {name:"a"} is a JavaScript object, not JSON.
  • Comments: neither // nor /* */ exist in the JSON standard. Configuration files that need comments should use JSON5 or YAML.
  • Missing escapes: double quotes inside a string need a backslash, and line breaks must be written as \n.
  • NaN, Infinity, undefined: none of these exist in JSON. Use null or a string representation.

The large-number trap

JSON itself does not limit number size, but JavaScript, which usually parses it, has a safe integer range. Values beyond that lose precision and the final digits change.

This bites when large database identifiers or snowflake-style IDs travel as JSON numbers. The server sends an exact value and the browser reads something slightly different.

The fix is to transmit large identifiers as strings rather than numbers, which is exactly why many APIs return IDs in quotes.

Choosing the right operation

While debugging, beautify. Formatting a single-line response from a log immediately reveals which fields are empty and how long the arrays are.

After editing a configuration file, validate. Discovering a syntax error after deployment means a service that will not start, so a check before committing pays for itself.

When request or response size matters, minify. Bear in mind that with gzip already enabled the gain from stripping whitespace is modest, so measure the actual transfer before optimising for it.

Tools covered in this guide

Copied