Skip to content

JSON Cheat Sheet

JSON (JavaScript Object Notation) is a lightweight, text-based, language-independent data interchange format. A JSON cheatsheet helps quickly reference JSON syntax, JSON data types, JSON escape sequences, and the application/json media type.

JSON Text

A JSON text is a complete JSON document, commonly represented as a single value such as an object or array.

JSON-text = value
value     = object / array / string / number / true / false / null
{ "id": 7, "tags": ["a", "b"], "active": true }

Object

An object is an unordered collection of name/value members enclosed in {} and separated by commas.

{}                            // empty object
{ "key": value }              // single member
{ "k1": value, "k2": value }  // multiple members
{
  "sequence": 52634,
  "employees": [
    { "first": "Anna", "last": "Smith", "loc": 1 },
    { "first": "John", "last": "Doe", "loc": 1 }
  ],
  "locations": [
    { "city": "New York", "type": "HQ", "key": 1 },
    { "city": "Los Angeles", "type": "Branch", "key": 2 }
  ]
}

Member

A member is a key/value pair inside an object, where the key is a string and the value is any JSON value.

"key" : value
{ "name": "Jane", "age": 39 }

Array

An array is an ordered list of values enclosed in [] and separated by commas.

[]                        // empty array
[value]                   // single element
[value, value, value]     // multiple elements
[
  "James",
  "Bond",
  42,
  "M",
  { "Germany": "Berlin" },
  true
]

Nested Objects and Arrays

Values can be objects or arrays, allowing nested structures.

{ "key": { ... } }
{ "key": [ ... ] }
{ "key": [ { ... }, { ... } ] }
{
  "indexes": { "Firstname": 0, "Lastname": 1, "Age": 2 },
  "user": ["James", "Bond", 42, "M", { "Germany": "Berlin" }, true]
}

String

A string is a sequence of Unicode characters enclosed in double quotes.

""                      // empty string
"some text"             // string
"escape: \" \\ \/"  // uses escapes
{ "message": "Line1\nLine2", "path": "C:\\tmp\\file.txt" }

String Escape Sequences

JSON strings use backslash escapes for quotes, backslash, and selected control characters.

\"   \\   \/   \b   \f   \n   \r   \t
\uXXXX
{
  "quote": "\"",
  "backslash": "\\",
  "tabbed": "A\tB",
  "unicode": "\u03C0"
}

Unicode Escape (\uXXXX)

\uXXXX encodes a Unicode code point using four hexadecimal digits.

\uXXXX
{ "pi": "\u03C0", "snowman": "\u2603" }

Number

A number is written in decimal and may include a fractional part and/or exponent.

123
123.456
1.234e-3
-10
-0.25
6.02E23
{ "int": 1234, "float": 1234.5678, "exp": 1.234e-3 }

Number Restrictions

JSON numbers are decimal and disallow superfluous leading zeros; octal/hex formats are not used in JSON number syntax.

0
10
-0.1
{ "ok": 0, "also_ok": 10, "negative_fraction": -0.1 }

Boolean

A boolean is either true or false.

true
false
{ "available": true, "archived": false }

Null

null represents an explicit null value.

null
{ "middleName": null }

Whitespace

Whitespace may appear between tokens and is not significant to the JSON data model.

{"a":1,"b":2}
{ "a" : 1 , "b" : 2 }
{ "name" : "John" , "age" : 30 }

Trailing Commas

A trailing comma after the final member/element is not part of standard JSON.

{ "k": "v" }     // valid
{ "k": "v", }    // invalid
[1, 2, 3]          // valid
[1, 2, 3,]         // invalid
{ "key": "val" }

Duplicate Keys

Object member names are strings; JSON objects are commonly treated as maps, and repeated keys are not distinct members in typical object models.

{ "key": 1, "key": 2 }
{ "key": 2 }

File Extension (.json)

A common filename extension for JSON documents is .json.

data.json
config.json
{ "file": "config.json" }

Media Type (application/json)

JSON payloads are associated with the application/json media type.

Content-Type: application/json
Accept: application/json
POST /api/items HTTP/1.1
Content-Type: application/json
Accept: application/json

{ "name": "widget", "qty": 3 }

Parsing JSON (JavaScript)

Parsing converts a JSON string into a JavaScript value.

JSON.parse(text[, reviver])
const obj = JSON.parse('{"a":1,"b":[true,null]}');
obj.b[0]; // true

Serializing JSON (JavaScript)

Serializing converts a JavaScript value into a JSON string.

JSON.stringify(value[, replacer[, space]])
const text = JSON.stringify({ a: 1, b: ["x", "y"] });
text; // '{"a":1,"b":["x","y"]}'

Parsing JSON (Python)

Parsing converts a JSON string into Python objects.

json.loads(s)
import json
obj = json.loads('{"a": 1, "b": [true, null]}'.replace("true", "true").replace("null", "null"))

Serializing JSON (Python)

Serializing converts Python objects into a JSON string.

json.dumps(obj)
import json
text = json.dumps({"a": 1, "b": ["x", "y"]})
print(text)

JSON Schema

JSON Schema is a JSON-based vocabulary used to describe and constrain the structure of JSON data.

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "id": { "type": "integer" },
    "name": { "type": "string" },
    "tags": { "type": "array", "items": { "type": "string" } }
  },
  "required": ["id", "name"]
}
{ "id": 7, "name": "item", "tags": ["a", "b"] }