JSON Basics

Core JSON syntax, valid values, root structures, and foundational examples for working with JSON documents.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Valid JSON roots

Understand what a complete JSON document can look like at the top level.

Root object

A JSON document can start with an object.

jsonANYjsonobjectroot
json
{
  "name": "Ada",
  "active": true
}

Objects are the most common top-level form for API payloads and configuration files.

Root array

A JSON document can start with an array.

jsonANYjsonarrayroot
json
[
  { "id": 1 },
  { "id": 2 }
]

Arrays are common for list-style payloads and exports.

Root string

A single JSON string is also valid JSON.

jsonANYjsonstringroot
json
"hello world"

Many developers expect only objects or arrays, but any valid JSON value can be the root.

Root number

A number can be the entire JSON document.

jsonANYjsonnumberroot
json
42

This is valid JSON even though it is less common in practice.

Root null

`null` is valid as a complete JSON document.

jsonANYjsonnullroot
json
null

Useful for understanding validators and parsers that accept any JSON value.

Core syntax rules

The most important rules people forget when writing JSON by hand.

Use double quotes for keys and strings

JSON requires double quotes, not single quotes.

jsonANYjsonquotessyntax
json
{
  "theme": "dark"
}

`'theme': 'dark'` is valid in some JavaScript contexts but invalid JSON.

Do not use trailing commas

Trailing commas make JSON invalid.

jsonANYjsoncommasyntax
json
{
  "name": "Ada",
  "role": "admin"
}

A trailing comma after the last property or array item is a frequent parse failure.

Do not add comments

Standard JSON does not support comments.

jsonANYjsoncommentssyntax
json
{
  "debug": false
}

Many configuration systems allow JSON-like files with comments, but plain JSON does not.

Pretty-print JSON with jq

Format JSON into a readable structure.

bashANYjsonjqpretty-print
bash
jq . input.json

A fast way to validate and pretty-print JSON from a file.

Validate JSON with Python

Check whether a file parses as valid JSON.

bashANYjsonpythonvalidate
bash
python -m json.tool input.json > /dev/null

Useful on machines where Python is available but `jq` is not.

Recommended next

No recommendations yet.