Developer

JSON Formatting Explained: Syntax, Common Errors & Best Practices

JSON (JavaScript Object Notation) is the lingua franca of APIs, config files, and data exchange. It is simple — but its strictness trips up developers daily. A single stray comma can break an entire payload. This guide covers the syntax rules, the errors that cause most parse failures, and the habits that keep your JSON clean and valid.

Key takeaways

  • JSON values are: string, number, boolean, null, object, or array.
  • Keys and strings must use double quotes — never single.
  • No trailing commas and no comments are allowed.
  • Validate and pretty-print before shipping to catch errors early.

The core syntax rules

Valid JSON follows a small, strict grammar:

  • Data is written as key/value pairs inside objects: { "key": "value" }
  • Keys are always double-quoted strings.
  • Values may be a string, number, true, false, null, an array [ ], or a nested object { }.
  • Items are separated by commas — but never after the last item.
  • The whole document must have exactly one root value.

A well-formed example:

{
  "name": "WorkIQ",
  "tools": 59,
  "free": true,
  "categories": ["pdf", "seo", "developer"],
  "owner": null
}

The 6 errors that break JSON most often

#MistakeFix
1Trailing comma after the last itemRemove the final comma
2Single quotes 'value'Use double quotes "value"
3Unquoted keys {name: "x"}Quote every key {"name":"x"}
4Comments // noteRemove them; JSON has no comments
5Missing/closing bracket mismatchBalance every { } and [ ]
6Unescaped characters in stringsEscape \", \\, and control chars

Paste, validate, and pretty-print

Spot the exact line of a syntax error and format messy JSON in one click — entirely in your browser.

Open the JSON Formatter →

Best practices for clean JSON

  1. Pick one indentation and stick to it. Two spaces is the common default; minify only for transport, not for reading.
  2. Use consistent key casing. camelCase or snake_case — choose one across your API.
  3. Keep it UTF-8. Save and serve JSON as UTF-8 to avoid mojibake with accented or non-Latin characters.
  4. Validate in CI. Lint JSON config files automatically so a bad comma never reaches production.
  5. Prefer arrays for lists, objects for records. Don't fake a list with numbered keys like "0", "1", "2".

JSON vs JavaScript objects

They look alike, but JSON is a data format, not code. JavaScript object literals allow single quotes, unquoted keys, comments, trailing commas, and functions — JSON allows none of these. When you JSON.parse() a string, it must obey JSON's stricter rules, which is why copy-pasting a JS object into a JSON file often fails.

Frequently asked questions

Can JSON have comments?

No. The standard forbids them. Use a dedicated field like _comment, or strip comments before parsing.

Are trailing commas allowed?

No. A comma after the final element is invalid and will throw a parse error in strict parsers.

Double or single quotes?

Always double quotes for keys and string values. Single quotes are not valid JSON.

Related tools

See our Disclaimer for how to use WorkIQ content and tools.