JSON has only a handful of rules, and a parser enforces every one of them. Break a single rule and the whole document is rejected. This lesson lists the rules, shows the mistakes that break them most often, and gives you a small function to test whether any text is valid JSON.
JSON data is built from two structures: an object (curly braces holding named values) and an array (square brackets holding a list). Everything else is a plain value inside them.
{
"title": "Dune",
"pages": 412,
"authors": ["Frank Herbert"],
"inPrint": true
}
{ } hold an object[ ] hold an arrayEvery key must be a string in double quotes. A JavaScript object lets you write title: "Dune", but JSON does not: it must be "title": "Dune". Single quotes are not allowed anywhere, for keys or for values.
A string value is also written in double quotes. To put special characters inside, use a backslash escape:
| Escape | Means |
|---|---|
\" | a double quote |
\\ | a backslash |
\n | a line break |
\t | a tab |
\u00e9 | a character by its code (here: the letter e with an accent) |
const text = '"She said \\"hi\\"\\nThen left."';
console.log(JSON.parse(text));
The doubled backslashes in the JavaScript line above are there for JavaScript itself: inside a JS string, \\n is a backslash followed by an n, which is exactly what JSON needs to see.
{ "a": 1, "b": 2 } valid
{ "a": 1, "b": 2, } invalid: comma after the last item
[1, 2, 3] valid
[1, 2, 3,] invalid
JavaScript lets you leave a trailing comma, and many people add one by habit. JSON does not.
JSON has no comment syntax at all: no //, no /* */. It also has no undefined, no functions, and no way to say "this value is a Date". Only the six kinds of values from the next lesson are allowed.
Spaces, tabs, and line breaks between pieces are ignored. These two documents are identical to a parser:
{"name":"Ava","age":16}
{
"name": "Ava",
"age": 16
}
Servers often send the compact version to save bytes. People usually format it with indentation so it is readable.
A JSON document does not have to be an object. A whole document can be an array, a string, a number, true, false, or null:
console.log(JSON.parse("[1, 2, 3]"));
console.log(JSON.parse('"hello"'));
console.log(JSON.parse("42") + 1);
console.log(JSON.parse("null"));
The easiest way to check JSON is to try to parse it. If the text breaks a rule, JSON.parse throws a SyntaxError, so a small try...catch function can answer yes or no (JS Errors covers try...catch):
function isValid(text) {
try {
JSON.parse(text);
return true;
} catch (error) {
return false;
}
}
console.log(isValid('{"name": "Ava"}'));
console.log(isValid("{name: 'Ava'}"));
console.log(isValid('{"a": 1,}'));
console.log(isValid('{"a": undefined}'));
console.log(isValid('// note\n{"a": 1}'));
| Text | Problem | Fix |
|---|---|---|
{name: "Ava"} | Unquoted key | Put the key in double quotes |
{"name": 'Ava'} | Single-quoted string | Use double quotes |
{"a": 1,} | Trailing comma | Remove the last comma |
{"a": 1 "b": 2} | Missing comma | Add a comma between pairs |
{"a": undefined} | undefined is not a JSON value | Use null or leave the key out |
{"a": 1} // note | Comment | Remove it: JSON has no comments |
true, false, and null must be lowercase. True and NULL are not JSON.
Which of these is valid JSON?
{name: "Ava"}{"name": 'Ava'}{"name": "Ava"}{"name": "Ava",}What does this log?
console.log(JSON.parse("42") + 1);
43. A JSON document can be just a number. JSON.parse("42") gives the number 42, and 42 + 1 is 43.Why is {"a": 1} // note not valid JSON?
// or /* */ makes the whole text invalid.isValid says true: {name: 'Ava', "grade": 10,}null, then parse it