DevTools.at

JSON Syntax: Rules and Examples

While JSON is designed to be simple, it has strict syntax rules that must be followed exactly. A single misplaced comma or wrong quote character can make your entire JSON invalid. This guide covers all the syntax rules you need to know to write valid JSON and quickly identify errors when they occur.

Basic Rules

JSON syntax is based on a subset of JavaScript, but with stricter rules that ensure cross-platform compatibility. Understanding these rules is essential for working with JSON effectively. The most important rule: JSON documents must use double quotes for strings, never single quotes. While JavaScript allows both 'hello' and "hello", JSON only accepts "hello". Similarly, all keys in JSON objects must be strings enclosed in double quotes. The JavaScript object {name: "John"} is valid JavaScript but invalid JSON; it must be written as {"name": "John"}. Whitespace in JSON is flexible. Spaces, tabs, and newlines can be added freely between tokens for readability without affecting the data. JSON has no concept of comments. Trailing commas are strictly prohibited. JSON is case-sensitive. The boolean values must be lowercase true and false, not True and False. Similarly, null must be lowercase. Encoding must be UTF-8 (preferred), UTF-16, or UTF-32.

Objects

Objects are the most commonly used JSON structure. They represent unordered collections of key-value pairs, similar to dictionaries in Python or hashmaps in Java. An object is enclosed in curly braces {} and contains zero or more key-value pairs. Each key-value pair consists of a key (which must be a string in double quotes), followed by a colon, followed by a value. Multiple pairs are separated by commas. Objects can be nested to any depth. Unlike JavaScript objects, JSON objects have no methods or functions—they contain only data. Key ordering is technically undefined by the specification, though many implementations preserve insertion order. Duplicate keys are technically allowed but strongly discouraged. Avoid them to ensure consistent behavior across platforms.

Arrays

Arrays in JSON represent ordered lists of values. Unlike objects which are collections of named properties, arrays are sequences where position matters. An array is enclosed in square brackets [] and contains zero or more values separated by commas. JSON arrays don't require all elements to be the same type, though in practice you'll usually want consistent types. Nested arrays are valid and useful for representing multi-dimensional data. Array order is preserved—when you serialize an array and parse it back, the elements will be in the same order.

Common Syntax Errors

Even experienced developers make JSON syntax errors. The single quotes error is extremely common among developers coming from JavaScript. Trailing commas cause countless parse errors when editing JSON. Unquoted keys work in JavaScript but not JSON. JSON has no comment syntax. Using undefined or NaN values fails because they don't exist in JSON. Mismatched brackets are easy to create in complex JSON. Escape sequence errors occur when special characters in strings aren't properly escaped. Incorrect number formats like leading zeros or hexadecimal notation cause subtle errors.

Try JSON Formatter

Put this knowledge into practice

Use JSON Formatter

Related Articles