JSON (JavaScript Object Notation)

JSON is a light-weight text-based open standard designed for human-readable data. It is the most widely used format for exchanging data on the web. It originates from the JavaScript language and is represented with two primary data structures: ordered lists (recognized as 'arrays') and name/value pairs (recognized as 'objects').

Example Object

var myObject = {
 "first": "John",
 "last": "Doe",
 "age": 39,
 "sex": "M",
 "salary": 70000,
 "registered": true
};

Example Array

var myArray = [
  { "name": "John Doe", "age": 29 }, 
  { "name": "Anna Smith", "age": 24 }, 
  { "name": "Peter Jones", "age": 39 }
];

Why JSON?

The JSON standard is language-independent and its data structures, arrays and objects, are universally recognized. These structures are supported in some way by nearly all modern programming languages and are familiar to nearly all programmers. These qualities make it an ideal format for data interchange on the web.

JSON vs XML

The XML specification does not match the data model for most programming languages which makes it slow and tedious for programmers to parse. Compared to JSON, XML has a low data-to-markup ratio which results in it being more difficult for humans to read and write.

Data Types

Number{ "myNum": 123.456 }
A series of numbers; decimals ok; double-precision floating-point format.

String{ "myString": "abcdef" }
A series of characters (letters, numbers, or symbols); double-quoted UTF-8 with backslash escaping.

Boolean{ "myBool": true }
True or false.

Array{ "myArray": [ "a", "b", "c", "d" ] }
Sequence of comma-separated values (any data type); enclosed in square brackets.

Object{ "myObject": { "id": 7 } };
Unordered collection of comma-separated key/value pairs; enclosed in curly braces; properties (keys) are distinct strings.

Null{ "myNull": null }
Variable with null (empty) value.

Unsupported Data Types

Undefinedvar myUndefined;
Variable with no value assigned.

Datevar myDate = new Date();
Object used to work with dates and times.

Errorvar myError = new Error();
Object containing information about errors.

Regular Expressionvar myRegEx = /json/i;
Variable containing a sequence of characters that form a search pattern.

Functionvar myFunction = function(){};
Variable containing a block of code designed to perform a particular task.

Examples

Mixed Data Types

var myObject = {
 "myNumber": 123.456,
 "myString": "abcdef",
 "myBool": true,
 "myArray": [ "a", "b", "c", "d" ],
 "myObject": { "id": 7 },
 "myNull": null,
 "myUndefined": undefined
};

JSON Cheat Sheet v1.3

Get your JSON up to speed fast with this excellent cheat.

Download the Cheet Sheet JSON Tutorial