Skip to content

Developer Tools

Why Properly Formatted JSON Matters

Trailing commas, single quotes and missing brackets break JSON silently. Learn the common mistakes and how to catch them before they hit production.

  • ToolsBench Team
  • 5 min read

JSON looks simple enough that it is easy to assume you can eyeball it for correctness. In practice, a single missing comma or a stray trailing comma will make an entire API response or config file fail to parse, and the error message you get back is rarely helpful about where the problem actually is.

What "valid JSON" actually requires

JSON is stricter than the JavaScript it resembles. The format only has a handful of rules, and most bugs come from breaking one of them:

  • Keys must be strings in double quotes.
  • Strings use double quotes, never single quotes.
  • No trailing commas after the last item in an object or array.
  • No comments. Standard JSON has no comment syntax.
  • Values can only be strings, numbers, booleans, null, objects, or arrays. There is no undefined, no functions, and no dates (dates are usually stored as strings).
  • Special characters in strings must be escaped, for example \" for a quote, \\ for a backslash, and \n for a newline.

The mistakes that show up constantly

Here is a snippet that looks plausible and is invalid:

{
  name: 'Ada',
  "tags": ["math", "code",],
  // active user
  "active": true,
}

It has an unquoted key, single quotes, a trailing comma in the array, a comment, and a trailing comma after the last property. Each of those is fine in a JavaScript object literal and each is an error in JSON. The valid version is:

{
  "name": "Ada",
  "tags": ["math", "code"],
  "active": true
}

Other classics: copying JSON out of a chat app or document that converted straight quotes into curly "smart" quotes, forgetting to escape a double quote inside a string, pasting two objects one after another without wrapping them in an array, and leaving an ellipsis or placeholder text in from a documentation example.

Why "it looked fine to me" is not enough

Structure is exactly what human eyes are bad at checking without indentation. A deeply nested object with inconsistent spacing hides mismatched brackets very well, and a minified payload on one long line is nearly unreadable. A formatter that fails loudly on invalid syntax, rather than an editor that simply displays whatever you typed, turns a long debugging session into a short one.

Formatting also helps people. Consistent indentation makes diffs in version control easier to review, because changes show up line by line rather than as one giant changed line.

How to catch problems early

  1. Validate before you paste. Run the text through the JSON validator before it goes into a config file, a test request, or a script. It reports whether the JSON is valid and shows the parser's error message when it is not.
  2. Format to read. The JSON formatter pretty-prints valid JSON with the indentation you choose so you can see the structure. It runs in your browser, so the data is not sent to a server, which is useful when the payload contains tokens or customer data (though it is still wise to strip secrets before sharing anything).
  3. Minify for production. Once the content is correct, the JSON minifier strips whitespace to make payloads smaller.
  4. Convert when needed. If you need to open an array of objects in a spreadsheet, JSON to CSV flattens it into rows and columns.
  5. Search inside large payloads. The regex tester can help you build a pattern to find or extract values, though for structured data a proper parser is usually safer than regular expressions.

How to decide where the bug is

  • Error at the very start: the text may not be JSON at all. An HTML error page, a BOM, or a log prefix is a common cause.
  • Error near the end: look for an unclosed bracket or brace earlier in the document.
  • Error at a comma or quote: check for trailing commas, single quotes, or an unescaped quote in a string.
  • Parses fine but your app breaks: the syntax is valid, so the problem is the data shape. Check field names, types (a number sent as a string), and missing keys.

Remember that the exact wording and position reported in an error message varies between parsers, so treat the position as a hint rather than the precise cause. The real mistake is often a little before the place the parser gives up.

Common mistakes

  • Treating JSON as a JavaScript object. They look alike but are not the same.
  • Editing JSON by hand in a plain text field without validating.
  • Using comments in config files when the consuming program only accepts strict JSON. Some tools accept a relaxed variant, but do not assume yours does.
  • Large numbers. Some parsers cannot represent very large integers precisely. If exact digits matter, such as an ID, store them as strings.
  • Committing secrets in a JSON config while tidying it up.

Quick answers

What is the difference between JSON and a JavaScript object?

A JavaScript object literal allows unquoted keys, single quotes, trailing commas, comments, and functions. JSON allows none of those. JSON is a text format, and JavaScript is one of many languages that can read and write it.

Why does my JSON have "Unexpected token" errors?

The parser found a character it did not expect, most often a trailing comma, single quote, comment, or a stray character before or after the data. Validate it to find where the parser stopped.

Does formatting JSON change its meaning?

No. Whitespace between tokens is ignored, so formatted and minified versions are equivalent. Formatting is purely for readability, and minifying is purely for size.

Should I send formatted or minified JSON from an API?

Minified is smaller on the wire, though if the response is compressed with gzip or similar, the difference shrinks. Many teams serve minified JSON and format it only when debugging.

Keep reading

More on Developer Tools