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 interchanging 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').

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.

Why Not 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.

Examples

Array

Array elements are generally of a basic type (number, string, boolean, or null), however can also be a nested array or object (see Data Structures). Elements are comma-delimited and contained within brackets.

var myArray = [ "John Doe", 29, true, null ];
var arrayNesting = [ [], {} ];
myArray[2]true

Array with objects

This array contains comma-delimited objects which each contain multiple comma-delimited key/value pairs. Objects within an array can be accessed using the array name and index.

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

Object

This object contains multiple comma-delimited key/value pairs. Object properties can be accessed using the the object name followed by a period and property name -or- can be accessed like an array using the property name in quotes (in place of an index).
var myObject = {
 "first": "John",
 "last": "Doe",
 "age": 39,
 "sex": "M",
 "salary": 70000,
 "registered": true
};
myObject.salary70000

myObject["salary"]70000

Object with nested array

This object contains multiple comma-delimited key/value pairs and a nested array. The nested array can be accessed with the object name, property or 'key' containing the array and an array index.

var myObject = {
 "first": "John",
 "last": "Doe",
 "age": 39,
 "sex": "M",
 "salary": 70000,
 "registered": true,
 "interests": [ "Reading", "Mountain Biking", "Hacking" ]
};
myObject.interests[0]Reading

myObject["interests"][0]Reading

Object with nested object

This object contains multiple comma-delimited key/value pairs and a nested object. To access an object within an object, property names or 'keys' can be chained together -or- property names can be used like an array in place of indexes.

var myObject = {
 "first": "John",
 "last": "Doe",
 "age": 39,
 "sex": "M",
 "salary": 70000,
 "registered": true,
 "favorites": {
  "color": "Blue",
  "sport": "Soccer",
  "food": "Spaghetti"
 } 
};
myObject.favorites.foodSpaghetti

myObject["favorites"]["food"]Spaghetti

Object with nested arrays and objects

This object is more complex and typical of what might come from an API. It contains key/value pairs, nested arrays and nested objects. Any of its elements can be accessed with a combination of the above techniques.

var myObject = {
  "first": "John",
  "last": "Doe",
  "age": 39,
  "sex": "M",
  "salary": 70000,
  "registered": true,
  "interests": [ "Reading", "Mountain Biking", "Hacking" ],
  "favorites": {
    "color": "Blue",
    "sport": "Soccer",
    "food": "Spaghetti"
  }, 
  "skills": [
    {
      "category": "JavaScript",
      "tests": [
        { "name": "One", "score": 90 },
        { "name": "Two", "score": 96 }
      ] 
    },
    {
      "category": "CouchDB",
      "tests": [
        { "name": "One", "score": 79 },
        { "name": "Two", "score": 84 }
      ] 
    },
    {
      "category": "Node.js",
      "tests": [
        { "name": "One", "score": 97 },
        { "name": "Two", "score": 93 }
      ] 
    }
  ]
};
myObject.skills[0].categoryJavaScript

myObject["skills"][0]["category"]JavaScript

myObject.skills[1].tests[0].score79

myObject["skills"][1]["tests"][0]["score"]79

Data Types

Numbervar myNum = 123.456;
series of numbers; decimals ok; double-precision floating-point format

Stringvar myString = "abcdef";
series of characters (letters, numbers, or symbols); double-quoted UTF-8 with backslash escaping

Booleanvar myBool = true;
true or false

Arrayvar myArray = [ "a", "b", "c", "d" ];
sequence of comma-separated values (any data type); enclosed in square brackets

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

Nullvar myNull = null;
variable with null (empty) value

Undefinedvar myUndefined;
variable with no value assigned

JSON Cheat Sheet v1.3

Get up to speed fast with the this excellent cheat sheet and never waste time Googling JSON references again.

Download the Cheet Sheet 100 Awesome Uses