JSON looks friendly. It reads like plain text, most of it is words and numbers you recognise, and yet a parser will reject a whole file over one wrong character. Almost every rejection comes down to the same cause: a value written in a way its data type does not allow. A number wearing quotes, a boolean with a capital letter, a key with no quotes at all.
The good news is that JSON has only six data types, and each one has a single strict way of being written. Learn those six shapes and you can read any JSON file. This guide walks through each type using the free JSON Formatter and Validator on Ovatly, which names the problem in plain words when something is wrong.
Contents
- Quick answer
- One small object, three types
- Strings: text in double quotes
- Numbers: written bare, with a precision catch
- Booleans and null: lowercase literals
- Arrays and objects: the two containers
- Keys are always strings
- How to check your JSON, step by step
- Worked example: one object with all six types
- Limitations and honest tradeoffs
- Common mistakes
- FAQs
- Conclusion
- References
Quick answer
JSON has six data types: string, number, boolean, null, array and object. Strings are written in double quotes, numbers are written bare with no quotes, booleans are the lowercase words true and false, null is the lowercase word null, arrays are ordered lists in square brackets, and objects are key and value pairs in curly braces, where every key is a string in double quotes. The JSON Formatter and Validator will pretty-print any JSON for reading, or validate it and tell you in plain words what is wrong, fully locally in your browser.
One small object, three types
Start with the example the tool itself uses:
{"name":"Ovatly","tools":250,"free":true}
Three values, three different types. "Ovatly" is a string, so it wears double quotes. 250 is a number, so it wears nothing. true is a boolean, written lowercase with no quotes. The words before the colons (name, tools, free) are the keys, and they follow the string rule too. Nearly every JSON file is this pattern repeated at larger sizes.
Strings: text in double quotes
A string is any text: a name, a city, a sentence, an empty space between quotes. Strings must be wrapped in double quotes. Single quotes are invalid in JSON, even though they work in much handwritten code.
Because the quotes mark where the text starts and ends, a string can hold characters that would mean something elsewhere in JSON, including digits. "250" in quotes is a string of three characters; 250 without quotes is a number, and systems treat the two very differently.
Numbers: written bare, with a precision catch
Numbers are written bare: no quotes, no commas inside them, no currency symbols. 250, -4 and 19.99 are all valid numbers. Writing "250" makes it a string, and writing 1,000 makes the file invalid, because the comma reads as a separator between two values.
There is one catch worth knowing before it bites you. Number precision follows JavaScript, so a whole number beyond 2^53 can lose exactness when the JSON is parsed and written out again. Very long numbers can be hit: an eighteen-digit account or ID number stored as a JSON number may come back slightly different after a round trip through a formatter. If a long digit sequence is an identifier rather than a quantity, store it as a string, where every character is preserved exactly.
Booleans and null: lowercase literals
A boolean is one of two words: true or false, always lowercase. True, TRUE and "true" in quotes are all wrong: the first two are not recognised, and the third is a string that merely looks like a boolean.
Null is a type of its own, written as the lowercase word null. It means no value: the field exists, and its value is deliberately nothing. That is different from an empty string, and different from leaving the field out entirely: systems reading the file can treat the three cases differently, so choose the one that matches what you mean.
Arrays and objects: the two containers
Four of the six types are single values. The other two are containers that hold values, including other containers.
An array is an ordered list in square brackets, separated by commas: a list of tags, a list of scores, a list of records. Order matters, and values in one array can mix types.
An object is a set of key and value pairs in curly braces. Each pair is a key, a colon, and a value of any of the six types. When a value is itself an object or an array, the JSON is nested, which is how real data describes things: a person object holding an address object.
Keys are always strings
Every key in a JSON object is a string, so every key must be written in double quotes. This is the rule handwritten JSON breaks most often, because JavaScript code allows bare keys and the two formats look alike. A parser does not allow it: an unquoted key makes the whole file invalid, and so does a trailing comma after the last pair. Strictness is deliberate: two programs exchanging JSON agree on exactly one grammar.
How to check your JSON, step by step
- Open the JSON Formatter and Validator and paste your JSON into the input.
- Click Validate only first. This checks the text without changing it, and names the problem in plain words if something is wrong.
- Fix what it names, then validate again: one error can hide the next.
- Once it validates, pretty-print it with a 2 or 4 space indent to read the structure, or minify it if you need the compact form. Because the tool runs fully locally, nothing you paste leaves your device.
Worked example: one object with all six types
As an illustration, here is the entry example extended to show all six types, adding a null, an array and a nested object:
{
"name": "Ovatly",
"tools": 250,
"free": true,
"owner": null,
"categories": ["calculators", "image tools", "text tools"],
"address": {
"city": "Lisbon",
"country": "Portugal"
}
}
Read it top to bottom: a string, a number, a boolean, a null, an array of three strings, and an object nested inside the object. Every JSON file, however large, is built from these six pieces and nothing else.
Limitations and honest tradeoffs
JSON's strictness is the tradeoff for its reliability. Comments are not allowed anywhere in a JSON file. Trailing commas, single quotes and unquoted keys are all invalid. The number precision limit above is a property of how the numbers are handled, not a bug a different formatter will fix. Finally, a validator checks that JSON is well formed; it cannot tell you whether the values are true or sensible. Valid JSON can still be wrong data.
Common mistakes
- Quoting numbers. "250" is a string. Any system expecting a number may reject it or sort it as text, where "100" comes before "99".
- Capitalising booleans. True and False belong to other languages. JSON only accepts lowercase true and false.
- Leaving keys unquoted. Every key is a string and needs double quotes, even a key that looks like a plain word.
- Adding a trailing comma. A comma after the last item in an object or array makes the file invalid.
- Storing long IDs as numbers. Beyond 2^53 a whole number can lose exactness when parsed and reformatted. Identifiers belong in quotes.
FAQs
How many data types does JSON have? Six: string, number, boolean, null, array and object. Arrays and objects are containers that hold values of the other types, including more arrays and objects nested inside them.
Do JSON keys need quotes? Yes. Every key in a JSON object is a string, so it must be written in double quotes. An unquoted key is one of the most common reasons a parser rejects a file.
Can I use single quotes in JSON? No. JSON strings must use double quotes. Single-quoted strings are invalid JSON, even though some programming languages accept them in their own code.
What is the difference between null and an empty string? An empty string is a string that happens to have no characters in it. Null is a separate type that means no value at all. Systems reading the file can treat the two differently, so pick the one that matches what you mean.
Why did my long ID number change when I reformatted JSON? Number precision follows JavaScript, and whole numbers beyond 2^53 can lose exactness when they are parsed and written out again. If a long identifier must survive exactly, store it as a string in quotes instead.
Conclusion
Six types, one strict shape each: that is the whole grammar. Once you can name a value's type and know how that type must be written, most JSON errors announce themselves. When you want a table instead of a data file, our companion guide on flattening nested JSON before converting it to CSV shows how nested objects become spreadsheet columns. For more guides in this series, see our Data and JSON Tools guides.
References
- JSON.org, the introducing JSON grammar and the six value types: https://www.json.org/json-en.html
- MDN Web Docs, on JSON syntax and data types: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON