EasyDeveloper

JSON

JSON Syntax Guide: The Six Value Types and the Rules That Catch You

Published 2026-08-15 · 8 min read

TL;DR: JSON has six value types - object, array, string, number, boolean and null - with double-quoted keys, and it rejects comments, trailing commas, single quotes and NaN.

JSON is a text format, not a data structure in memory. The same bytes parse to the same value in every language, which is exactly why it became the interchange format of the web. This guide walks the grammar: the six types, the exact rules for strings and numbers, and the boundary cases that trip up real documents. The rules below follow the JSON specification.

What are the six JSON value types?

TypeExampleRules
Object{"key": "value"}An unordered set of key-value pairs; keys are strings; values are any type
Array[1, 2, 3]An ordered list of values separated by commas
String"text"Double-quoted; supports escape sequences
Number3.14Integer or decimal, no leading zero, no NaN or Infinity
BooleantrueThe literal true or false
NullnullThe literal null

How do JSON objects work?

An object is a collection of key-value pairs inside { }, separated by commas. Keys must be strings in double quotes: {"name": "Ada"} is valid, {name: "Ada"} is not. Values can be any of the six types, including nested objects and arrays:

{
  "user": {
    "name": "Ada",
    "roles": ["admin", "editor"],
    "active": true
  }
}

Object key order is not semantically meaningful in the grammar. Parsers may preserve insertion order, but JSON itself does not promise it. If order matters, use an array of objects with an explicit key.

How do JSON arrays work?

An array is an ordered list of values inside [ ] and separated by commas. The values do not have to share a type, which is unusual compared with typed formats: [1, "two", true, null] is valid JSON. Arrays nest like objects, and the same trailing-comma rule applies: the last element must not be followed by a comma.

[1, "two", true, null]     // valid, mixed types
[1, 2, 3,]               // invalid, trailing comma
[[]]                     // valid, an array containing an array

Arrays are the JSON way to represent ordered data, since objects do not promise order. If you need a list of records, use an array of objects; if you need a keyed lookup, use an object.

What are the rules for JSON strings?

Strings are enclosed in double quotes. To include a quote or a control character, use an escape. Unicode characters can be written directly as UTF-8 or as an escape such as \u0041. Two rules catch most people: single quotes are not string delimiters, and a literal newline inside a string is not allowed, so a line break has to be written as the two characters backslash-n.

EscapeMeaning
\"Double quote
\\Backslash
\/Forward slash
\bBackspace
\fForm feed
\nNewline
\rCarriage return
\tTab
\uXXXXUnicode code point, for example \u0041

Any other backslash sequence is an error. A string that contains a lone backslash before an ordinary letter is invalid even though it looks harmless, because the grammar only allows the escapes in the table plus a four-digit unicode escape.

What are the rules for JSON numbers?

Numbers are decimal: an optional minus sign, then an integer part, optional fraction and optional exponent. There is no leading zero, because 01 is invalid while 1 is not, and no special values, because NaN, Infinity and -Infinity are not JSON:

0        // valid
-1.5e3   // valid, -1500
01       // invalid
.5       // invalid, use 0.5
1.       // invalid, use 1 or 1.0
NaN      // invalid

What is not allowed in JSON?

  • Comments: // and /* */ are not part of the grammar.
  • Trailing commas: a comma after the last element of an object or array.
  • Single-quoted strings and unquoted keys.
  • Undefined, NaN, Infinity, functions and dates as values.

JavaScript object literals allow most of these, which is why a file that is valid JavaScript can be invalid JSON. The JSON validator is the fastest way to check.

How are booleans and null written?

The last two of the six value types are the keywords true, false and null. They are case-sensitive: true is a value, but True, TRUE and true are three different things, and only the lowercase form parses. There is no separate undefined type and no empty value - null is the explicit way to say a key has no data.

"retry": true
"enabled": false
"error": null

A missing key and a key set to null are not the same thing. A missing key says the field does not exist; null says it exists with no value. APIs treat those two cases differently, so choose deliberately.

What are the edge cases that break real documents?

Large integers lose precision in JavaScript because numbers are IEEE 754 doubles; values above 2^53 are not exactly representable, so a large ID stored as a number can silently round. The fix is to send identifiers as strings. The other common edge case is nested depth: parsers impose a limit, and a document nested a few thousand levels deep will be rejected even though the grammar allows it.

Whitespace is ignored between tokens - spaces, tabs and newlines anywhere except inside strings. That is what makes formatting safe and what makes minification lossless.

Frequently Asked Questions

Is JSON a subset of JavaScript?

Mostly. The value grammar matches, but JSON is stricter: keys must be quoted, trailing commas are rejected, and JSON5 and JSONC extend it in incompatible ways. Treat JSON as its own grammar.

Can JSON store a date?

There is no date type. Dates are conventionally stored as ISO 8601 strings or as a Unix timestamp number. The receiving application decides which.

Is key order in JSON objects guaranteed?

No. The grammar does not define order. Some parsers preserve insertion order as an implementation detail, but you should not rely on it.

What is the maximum depth of a JSON document?

The grammar allows any depth; parsers impose practical limits. A document nested thousands of levels deep will be rejected by most parsers.