/tutorials/json-syntax-explained
JSON Syntax Explained: What Makes JSON Valid or Invalid
The small set of rules that decide whether JSON parses correctly, and the mistakes that most often break it.
5 min read
Run JSON FormatterRelated tools
What JSON actually is
JSON, short for JavaScript Object Notation, is a plain text format for representing structured data, built around just a handful of data types: objects, arrays, strings, numbers, booleans and null. Its entire appeal is how small and strict the rule set is, which makes it easy for software in almost any programming language to read and write reliably.
The rules that actually matter
A small, valid JSON document
{
"name": "Frabs",
"free": true,
"toolCount": 25,
"founder": null,
"categories": ["website", "network", "security"]
}- Every key and every string value must be wrapped in double quotes. Single quotes are not valid JSON, even though they are perfectly valid in JavaScript itself.
- A comma separates items in an object or array, but there must never be a trailing comma after the final item.
- Numbers are written without quotes and without a leading plus sign, and there is no separate type for integers versus decimals.
- true, false and null are written exactly like that, lowercase and without quotes.
- Comments are not allowed anywhere in JSON, which surprises people coming from JavaScript or most programming languages, where comments are taken for granted.
The mistakes that break JSON most often
- A trailing comma after the last item in an object or array, often left behind after removing the item that used to come after it.
- Using single quotes instead of double quotes, usually from copying a JavaScript object literal directly rather than actual JSON.
- An unescaped double quote inside a string value, which needs to be written as a backslash followed by a quote instead.
- A missing closing brace or bracket, especially in deeply nested data where it is easy to lose track of how many levels need closing.
Reading a parser's error message
Most JSON parsers report the exact character position where they gave up, not necessarily where the actual mistake is. A missing comma, for instance, is usually only noticed once the parser reaches the very next token and finds it does not belong there, which can be several lines after the real problem. When an error mentions a specific position, it is worth working backward from that point rather than assuming it points directly at the mistake itself.
Using the JSON Formatter
The JSON Formatter validates the text you paste in and reports a clear error with line and column numbers when something does not parse, alongside options to format it with readable indentation or minify it down to a single line, entirely in your browser without sending the data anywhere.
Related tutorials
