JSON (JavaScript Object Notation) is a lightweight data-interchange format for representing data as text. As the name suggests, it derives from JavaScript's object notation, but today it is a language-independent standard (RFC 8259 / ECMA-404) used widely for Web APIs, configuration files, logs and more. In this article we organize JSON's data types and syntax rules, the errors people commonly stumble over, and tips for formatting and validation, all with real examples.
1. What is JSON?
JSON is a format for representing structured data as a string that both humans and machines can read. It was originally devised based on JavaScript's object notation, but because the syntax is simple and standard libraries exist in many languages, it has become the de facto standard for data interchange, starting with communication between servers and browsers (Web APIs).
JSON itself is just text. The file extension is .json, and over HTTP it is exchanged with the MIME type application/json. As a rule, the character encoding used is UTF-8.
2. The six JSON data types
The values that JSON can represent are the following six kinds. You nest them to build any structure you like.
| Data type | Description | Example |
|---|---|---|
| Object | A set of key/value pairs, enclosed in braces { }. Keys must be strings. | {"name":"Taro","age":30} |
| Array | An ordered list of values, enclosed in brackets [ ]. Element types may be mixed. | [1,"a",true] |
| String | A sequence of characters in double quotes. Escapes such as \n and \" can be used. | "hello" |
| Number | Integer, decimal or exponential notation. Decimal only. Leading zeros and a + sign are not allowed. | 42 / -3.14 / 1e3 |
| Boolean | A logical value, written in all lowercase. | true / false |
| null | Represents the absence of a value. Lowercase null. | null |
For example, {"user":{"name":"Taro","tags":["admin",null],"active":true}} combines objects and arrays to express hierarchical data.
3. The basics of the syntax rules
JSON defines strictly what writing is permitted. The most important points are as follows.
- Keys must be strings in double quotes:
{"name":"Taro"}is correct, while{name:"Taro"}is invalid. - Strings use double quotes only: single quotes
'...'cannot be used. - No trailing commas: you cannot put a comma after the last element of an array or object.
- No comments:
//and/* */do not exist in standard JSON. - Numbers are decimal only: hexadecimal, leading zeros, a trailing period (
1.),NaNandInfinityare all disallowed.
Many of these are loosely permitted in JavaScript object literals, so be careful not to confuse the two. JSON is, after all, an independent data specification.
4. Common errors
The table below summarizes the typical patterns that cause a parse to fail. When you hit an error, suspect these first.
| Cause | Invalid example | Correct form |
|---|---|---|
| Trailing comma | {"a":1,"b":2,} | {"a":1,"b":2} |
| Single quotes | {'a':'x'} | {"a":"x"} |
| Unquoted key | {a:1} | {"a":1} |
| Full-width space / symbol | {"a":1} (full-width colon) | {"a":1} (half-width colon) |
| BOM (invisible leading byte) | U+FEFF at the start of the file | Save as UTF-8 without BOM |
| NaN / Infinity | {"x":NaN} | Represent with {"x":null}, etc. |
| Comment inserted | {"a":1} // note | Remove the comment |
5. Formatting (Pretty) and minifying (Minify)
The same JSON data can look different depending on how whitespace and line breaks are inserted. The two representative operations are formatting (pretty print) and minifying.
- Formatting: adds indentation and line breaks to make the hierarchy easy to read. For example,
{ "a": 1, "b": [2, 3] }is expanded over multiple lines. Suited to reviews, debugging and saving configuration files. - Minifying: removes all whitespace and line breaks that do not affect meaning, putting it on one line such as
{"a":1,"b":[2,3]}. Because the size shrinks, it suits API responses and efficient transmission or storage.
Formatting and minifying only change the appearance; the meaning of the data is identical. Format it when humans read it, minify it when machines transmit or store it, switching by purpose with no problem.
6. How to think about validation
There are two perspectives on JSON validation, at different levels.
- Syntax validation: whether it can be parsed as valid JSON at all. Check that there are no errors such as trailing commas or unquoted keys, like those listed in Chapter 4.
- Schema validation: in addition to being parseable, whether it has the expected structure and types. For example, you define constraints such as "
ageis a required number" or "tagsis an array of strings" with something like JSON Schema and check against them.
First confirm the syntax is correct with a tool, and if the data is used in production, going as far as schema validation is the safe choice. When you want a quick check at hand, the online tool below is handy.
Free Tool Check it with the JSON Formatter & Validator Just paste your JSON to format (pretty) or minify it, and see where syntax errors are. It runs entirely in your browser, and no data is sent.Frequently Asked Questions (FAQ)
Can you write comments in JSON?
Standard JSON (RFC 8259) has no syntax for comments. Writing // or /* */ causes a parse error. If you need comments, for example in a configuration file, consider extended formats such as JSON5 or JSONC, or hold the explanation as a string in a separate field. For data interchange, the rule of thumb is to include no comments.
Why does a trailing comma cause an error?
Because the JSON grammar does not allow a comma after the last element of an array or object. {"a":1,} and [1,2,] are invalid. JavaScript object literals do allow trailing commas, which is a common source of confusion, but JSON is a separate specification, so always remove the trailing comma.
When should I use formatting (Pretty) vs. minifying (Minify)?
Formatting adds indentation and line breaks to make the data easier for humans to read, which suits reviews, debugging and saving configuration files. Minifying removes whitespace and line breaks to keep the size to a minimum, which suits API responses and efficient transmission or storage. Both represent the same data, so you can switch freely depending on the purpose.