JSON Formatting and Validation: A Practical Guide
The rules of valid JSON, the errors that break it most often, and when to format or minify.
What JSON is
JSON (JavaScript Object Notation) is a text format for structured data. It is used by almost every web API, many configuration files and a lot of databases. The format is defined in RFC 8259 and is intentionally small. That simplicity is why it is everywhere, but it also means the rules are strict.
The building blocks
A JSON value is one of six types:
- Object: key–value pairs in braces, for example
{"name": "Ada"}. Keys must be strings. - Array: an ordered list in square brackets, for example
[1, 2, 3]. - String: text in double quotes, with escapes such as
\nand\". - Number: for example
42,-3.5or1e6, with no leading zeros and noNaNorInfinity. - Boolean:
trueorfalse, in lowercase. - Null:
null.
The errors that break JSON most often
Most invalid JSON comes from habits carried over from JavaScript or other languages. Paste your data into the JSON Formatter and it will point to the problem. These are the usual suspects:
Trailing commas
{"a": 1, "b": 2,} is invalid. Remove the comma after the last item.
Single quotes
{'a': 'text'} is invalid. Strings and keys must use double quotes.
Unquoted keys
{a: 1} works in JavaScript but not in JSON. Write {"a": 1}.
Comments
Standard JSON has no comments. Some tools accept variants such as JSONC or JSON5, but a standard parser will reject // notes.
Unescaped characters in strings
Line breaks, tabs and double quotes inside a string must be escaped as \n, \t and \".
Wrong literal values
True, None, undefined and NaN are not JSON. Use true, null or a string.
Pretty-printing: for people
Formatted, or “pretty-printed”, JSON puts each value on its own line and indents nested structures. It is much easier to read, review and compare in version control. Two spaces is the most common indentation in JavaScript projects, while four spaces is common in Python projects. The best choice is whatever your team already uses.
Minifying: for machines
Whitespace outside strings is meaningless to a parser. The JSON Minifier removes it to make payloads smaller, which helps when JSON is sent over a network, stored in a single field or embedded in a URL. Most web servers also compress responses with gzip or Brotli, which removes much of the difference, so minifying matters most where compression is not available.
Precision and duplicate keys
JSON itself allows numbers of any size, but JavaScript stores numbers as 64-bit floating point. Integers larger than 9,007,199,254,740,991 can lose precision when parsed in a browser. If you work with large IDs, send them as strings. Duplicate keys are also allowed by the grammar, but most parsers keep only the last value, so avoid them.
Keep sensitive data local
API responses often contain tokens, email addresses or customer data. Be careful about pasting them into online tools that upload your input. TextForge formats and validates JSON entirely in your browser, so the data never leaves your device.
Checklist
- Double quotes for all strings and keys.
- No trailing commas and no comments.
- Lowercase
true,falseandnull. - Pretty-print for reading and reviewing, and minify for transport and storage.
- Send very large integers as strings.