JSON Cheatsheet

A quick reference guide for JSON syntax, data types, and common patterns.

๐Ÿ“ Basic Syntax

{ }Object - key-value pairs
[ ]Array - ordered list
" "String - double quotes only
:Separates key from value
,Separates items (no trailing comma!)

๐Ÿ”ค Data Types

String"hello"
Number42, 3.14, -17, 2e10
Booleantrue, false
Nullnull
Array[1, 2, 3]
Object{"key": "value"}

๐Ÿ”ก Escape Sequences

\"Quote
\\Backslash
\nNewline
\tTab
\rCarriage return
\uXXXXUnicode

๐Ÿšซ Not Allowed in JSON

โœ—'single quotes'Use double quotes
โœ—// commentsNo comments
โœ—[1, 2, 3,]No trailing comma
โœ—undefinedUse null instead
โœ—{key: "value"}Keys must be quoted

๐Ÿ“ฆ Object Example

{
  "name": "John Doe",
  "age": 30,
  "isActive": true,
  "email": null,
  "tags": ["developer", "writer"],
  "address": {
    "city": "Istanbul",
    "country": "Turkey"
  }
}

๐Ÿ“‹ Array Examples

Strings
["apple", "banana", "cherry"]
Numbers
[1, 2, 3, 4, 5]
Objects
[{"id": 1}, {"id": 2}]
Mixed
["text", 42, true, null]

๐Ÿ’ป JavaScript Methods

JSON.parse()

// String โ†’ Object
const obj = JSON.parse('{"a":1}');
// obj.a === 1

JSON.stringify()

// Object โ†’ String
const str = JSON.stringify({a:1});
// str === '{"a":1}'

Pretty Print

JSON.stringify(obj, null, 2);
// 2-space indentation

Reviver Function

JSON.parse(str, (k, v) => {
  if (k === 'date') return new Date(v);
  return v;
});

๐ŸŽฏ Common Patterns

API Response

{
  "data": [...],
  "meta": {
    "total": 100,
    "page": 1
  }
}

Config File

{
  "name": "my-app",
  "version": "1.0.0",
  "scripts": {
    "build": "vite build"
  }
}

GeoJSON

{
  "type": "Point",
  "coordinates": [
    29.0, 41.0
  ]
}