JSON Format: Syntax, Types, and Examples

Learn the six JSON value types, the rules for valid syntax, and how formatting makes structured data easier to read.

JSON is a text format for exchanging structured data. A JSON document contains one value: an object, an array, a string, a number, a boolean, or null. It does not have to start with a curly brace.

A complete example

{
  "name": "Alex",
  "projects": 3,
  "available": true,
  "skills": ["TypeScript", "Astro"],
  "settings": { "theme": "light" },
  "lastError": null
}

This document uses all six value types. name is a string, projects is a number, available is a boolean, skills is an array, settings is an object, and lastError is null.

The rules that matter

  • Put object property names and string values in double quotes. Single quotes are not valid JSON string delimiters.
  • Separate a property name from its value with a colon.
  • Separate object members and array items with commas. Do not put a comma after the last one.
  • Write true, false, and null in lowercase, without quotes.
  • Use square brackets for arrays and curly braces for objects.
  • Escape double quotes, backslashes, and control characters inside strings.

For example, "first\nsecond" is a string with an escaped line break. An actual, unescaped line break inside a quoted string is invalid.

Formatting versus validation

These two documents represent the same structure and values:

{ "name": "Alex", "projects": 3 }
{
  "name": "Alex",
  "projects": 3
}

Formatting changes whitespace outside strings. Validation checks whether the document obeys the grammar. A JSON formatter should not silently repair invalid syntax or change the values to make a document parse.

JSON Schema validation is another task: it checks whether a document follows rules you supply, such as a required name property or a minimum project count. A syntactically valid document can fail a schema check.

Numbers need care

JSON numbers can include a minus sign, fractional digits, and an exponent. Examples include -4, 1.25, and 2e3. Leading zeros such as 007, a leading plus sign, NaN, and Infinity are not valid JSON number tokens.

A parser can still have numeric limits. A large valid integer such as 9007199254740993 can lose precision when converted to a JavaScript number. This tool preserves the original number tokens when formatting or minifying instead of converting them through JavaScript numbers.

What JSON does not include

Strict JSON has no comments, undefined values, functions, or native date type. Represent dates as strings using a convention agreed upon by the producer and consumer. JSONC and JSON5 are related formats with different rules; their extra syntax is not automatically valid JSON.

Continue with arrays and objects or common parse errors. The formal syntax and interoperability guidance are defined in RFC 8259.

Already have valid JSON? Make it readable