JSON Cheat Sheet

JSON Cheat Sheet

A JSON cheat sheet.

Data Types

number
var myNum = 123.456;
Series of numbers; decimals ok; double­-pr­ecision floati­ng-­point format
string
var myString = «­abc­def­»;
Series of characters (letters, numbers, or symbols); double­-quoted UTF-8 with backslash escaping
boolean
var myBool = true;
true or false
array
var myArray = [ «­a», «­b», «­c», «­d» ];
sequence of comma-­sep­arated values (any data type); enclosed in square brackets
object
var myObject = { «­id»: 7 };
unordered collection of comma-­sep­arated key/value pairs; enclosed in curly braces; properties (keys) are distinct strings
null
var myNull = null;
variable with null (empty) value
undefined
var myUnde­fined;
variable with no value assigne

Objects

var myObject = {
«first»: «John»,
«last»: «Doe»,
«age»: 39,
«sex»: «male»,
«salary»: 70000,
«registered»: true
};

Access object properties

myObje­ct.sex
returns «­mal­e»
myObje­ct[­»­age­»]
returns 39
myObje­ct[0]
returns «­Joh­n»
myObje­ct.s­om­ething
returns undefined
myObje­ct[6]
returns undefined

Array of objects

var myArray = [
{
«first»: «John»,
«last»: «Doe»,
«age»: 39,
«sex»: «male»,
«salary»: 70000,
«registered»: true
},
{
«first»: «Jane»,
«last»: «Smith»,
«age»: 42,
«sex»: «female»,
«salary»: 80000,
«registered»: true
},
{
«first»: «Amy»,
«last»: «Burnquist»,
«age»: 29,
«sex»: «female»,
«salary»: 60000,
«registered»: false
}
];

Access array elements

myArray[0]
returns { «­fis­t»: «­Joh­n», «­las­t»: «­Doe­» … }
myArray[1]
returns { «­fis­t»: «­Jan­e», «­las­t»: «­Smi­th» … }
myArra­y[1­].first
returns «­Jan­e»
myArra­y[1][2]
returns 42
myArra­y[2­].r­egi­stered
returns false
myArray[3]
returns undefined
myArra­y[3­].sex
error: «­cannot read proper­ty…»

Arrays

var myArray = [
«John»,
«Doe»,
39,
«M»,
70000,
true
];

Access array elements

myArray[1]
returns «­Doe­»
myArray[5]
returns true
myArray[6]
returns undefined

Nested objects and arrays

var myObject = {
«ref»: {
«first»: 0,
«last»: 1,
«age»: 2,
«sex»: 3,
«salary»: 4,
«registered»: 5
},
«jdoe1»: [
«John»,
«Doe»,
39,
«male»,
70000,
true
],
«jsmith1»: [
«Jane»,
«Smith»,
42,
«female»,
80000,
true
]
};

Access nested elements

myObje­ct.r­ef.first
returns 0
myObje­ct.j­doe1
returns [ «­Joh­n», «­Doe­», 39 … ]
myObje­ct[2]
returns [ «­Jan­e», «­Smi­th», 42 … ]
myObje­ct.j­sm­ith1[3]
returns «­fem­ale­»
myObje­ct[­1][5]
returns true
myObje­ct.j­do­e1[­myO­bje­ct.r­ef.last]
returns «­Doe­»
myObje­ct.j­sm­ith­1[m­yOb­jec­t.r­ef.age]
returns 42

Download the JSON Cheat Sheet

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!