JSON Cheat Sheet

Quick reference for JSON syntax, data types, common patterns, and gotchas. Validation rules, nested structures, and working with JSON in JavaScript, Python, and APIs.

Data Types Syntax Rules Common Patterns JavaScript Python JSON vs Alternatives Common Gotchas

Data Types

"string" Text (must use double quotes, not single)
42 / 3.14 / -1 / 1.5e10 Number (integer, float, negative, scientific)
true / false Boolean
null Null (empty value)
{"key": "value"} Object (unordered key-value pairs)
[1, 2, 3] Array (ordered list of values)

Syntax Rules

Keys must be double-quoted strings "name": "John" ✓ — name: "John" ✗
No trailing commas {"a": 1, "b": 2} ✓ — {"a": 1, "b": 2,} ✗
No comments JSON does not support // or /* */ comments
No single quotes "hello" ✓ — 'hello' ✗
No undefined Use null instead of undefined
No NaN or Infinity Use null or string representation instead
Strings must escape special chars Use \n \t \\ \" for newline, tab, backslash, quote
Root must be object or array {"key":"val"} ✓ — plain text ✗

Common Patterns

{"users": [{"id": 1, "name": "Alice"}]} Array of objects (most common API response)
{"data": {...}, "meta": {"page": 1}} Envelope pattern (data + metadata)
{"error": {"code": 404, "message": "Not found"}} Error response pattern
{"type": "user", "id": "123", "attributes": {...}} JSON:API resource format
[{"op": "replace", "path": "/name", "value": "Bob"}] JSON Patch (RFC 6902)
{"$schema": "http://json-schema.org/draft-07/schema#"} JSON Schema definition

JavaScript

JSON.parse(string) Parse JSON string → JavaScript object
JSON.stringify(obj) Convert object → JSON string
JSON.stringify(obj, null, 2) Pretty-print with 2-space indentation
JSON.stringify(obj, replacer) Custom serialization with replacer function
structuredClone(obj) Deep clone (modern alternative to JSON round-trip)
fetch(url).then(r => r.json()) Parse JSON from API response
Response.json(data) Create JSON response (in APIs/Workers)

Python

import json Import JSON module
json.loads(string) Parse JSON string → Python dict
json.dumps(obj) Convert dict → JSON string
json.dumps(obj, indent=2) Pretty-print with indentation
json.load(file) Read JSON from file object
json.dump(obj, file) Write JSON to file object
json.dumps(obj, default=str) Handle non-serializable types (dates, etc.)

JSON vs Alternatives

JSON vs YAML YAML is a superset of JSON. More readable, supports comments, but whitespace-sensitive
JSON vs TOML TOML is better for config files. Supports dates natively, easier to edit by hand
JSON vs XML JSON is lighter and easier to parse. XML has schemas and namespaces for complex documents
JSON vs CSV CSV is for flat tabular data. JSON handles nested/hierarchical structures
JSON vs MessagePack MessagePack is binary JSON — smaller and faster to parse, not human-readable
JSON vs Protocol Buffers Protobuf is strongly typed and much smaller. Requires schema definition (.proto files)

Common Gotchas

Number precision JSON numbers can lose precision beyond 2^53 (use strings for big IDs)
Date handling JSON has no date type — use ISO 8601 strings: "2024-01-15T09:30:00Z"
Encoding JSON must be UTF-8 (RFC 8259). BOM is allowed but not recommended
Key ordering JSON object key order is not guaranteed. Do not depend on it
Duplicate keys Technically allowed but behavior is undefined. Avoid them
Empty vs null "" (empty string) and null are different. Choose one convention and be consistent

Try It Live

Test these patterns with our free JSON Formatter. No signup needed.

Open JSON Formatter →
Step-by-Step Guide

How to Format JSON Online

Read Guide →

More Cheat Sheets