JSON Cheat Sheet

Quick reference guide for JSON syntax, methods, and best practices

Basic Syntax

{
  "string": "value",
  "number": 42,
  "float": 3.14,
  "boolean": true,
  "null": null,
  "array": [1, 2, 3],
  "object": {"nested": true}
}
  • ✅ Double quotes for strings
  • ✅ No trailing commas
  • ✅ No comments allowed
  • ✅ Keys must be strings

Data Types

  • "string" - Text in double quotes
  • 123 - Number (int or float)
  • true/false - Boolean
  • null - Null value
  • [] - Array
  • {} - Object
// Example
{
  "name": "John",      // string
  "age": 30,           // number
  "active": true,      // boolean
  "spouse": null,      // null
  "hobbies": [],       // array
  "address": {}        // object
}

Arrays

// Simple array
[1, 2, 3, 4, 5]

// Mixed types
["text", 123, true, null]

// Array of objects
[
  {"id": 1, "name": "John"},
  {"id": 2, "name": "Jane"}
]

// Nested arrays
[[1, 2], [3, 4]]

Objects

// Simple object
{"key": "value"}

// Nested object
{
  "user": {
    "name": "John",
    "address": {
      "city": "NYC"
    }
  }
}

// Object with array
{
  "users": [
    {"id": 1},
    {"id": 2}
  ]
}

JavaScript Methods

// Parse JSON string
const obj = JSON.parse(jsonString);

// Convert to JSON string
const str = JSON.stringify(obj);

// Pretty print (2-space indent)
JSON.stringify(obj, null, 2);

// With replacer function
JSON.stringify(obj, (k, v) => 
  k === 'password' ? undefined : v
);

// Parse with reviver
JSON.parse(str, (k, v) => 
  k === 'date' ? new Date(v) : v
);

Common Errors

  • ❌ Single quotes: 'value'
  • ✅ Double quotes: "value"
  • ❌ Trailing comma: {"a": 1,}
  • ✅ No trailing comma: {"a": 1}
  • ❌ Unquoted keys: {key: "value"}
  • ✅ Quoted keys: {"key": "value"}
  • ❌ Comments: // comment
  • ✅ No comments allowed in JSON

Escape Sequences

  • \" - Double quote
  • \\ - Backslash
  • \/ - Forward slash
  • \n - Newline
  • \r - Carriage return
  • \t - Tab
  • \b - Backspace
  • \f - Form feed
  • \uXXXX - Unicode
"He said \"Hello\""
"Path: C:\\Users\\Name"
"Line 1\nLine 2"

Best Practices

  • ✅ Use consistent indentation
  • ✅ Validate before sending
  • ✅ Use schema validation
  • ✅ Minify for production
  • ✅ Enable gzip compression
  • ✅ Handle parse errors
  • ✅ Never use eval() to parse
  • ✅ Sanitize user input

Common HTTP Status Codes

  • 200 OK - Success
  • 201 Created - Resource created
  • 204 No Content - Success, no body
  • 400 Bad Request - Invalid JSON
  • 401 Unauthorized - Auth required
  • 404 Not Found - Not found
  • 422 Unprocessable - Validation fail
  • 500 Server Error - Server issue

HTTP Headers

// Request
Content-Type: application/json
Accept: application/json

// Response
Content-Type: application/json
Cache-Control: max-age=3600

// Fetch example
fetch(url, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(data)
})

JSONPath Syntax

  • $ - Root element
  • @ - Current element
  • . - Child element
  • .. - Recursive descent
  • * - Wildcard
  • [] - Array subscript
  • [,] - Union operator
  • [?()] - Filter expression
$.store.book[0].title
$.store.book[*].author
$..author
$.store.book[?(@.price < 10)]

Performance Tips

  • Minify JSON in production
  • Enable gzip/brotli compression
  • Use pagination for large datasets
  • Cache parsed JSON objects
  • Stream large files
  • Use Web Workers for parsing
  • Consider binary formats (MessagePack)
  • Limit nesting depth

Try Our JSON Tools

30+ professional tools for all your JSON needs

Validate JSON
Format JSON JSON to TypeScript Read Blog