JSON Data Format

By Xah Lee. Date: . Last updated: .

What is JSON

JSON file name extension

JSON file has file name extension .json.

Special values in JSON

json has the following special literal values

JSON Code Example

Simple array

[1, 6.9, "abc"]

Simple array with special values

[1, 6.9, "abc", true, false, null]

Simple object

{
 "joe": 42,
 "mary": 20,
 "jenny": 25
}

Nested array and object

{
  "name": "Joe",
  "alive": true,
  "age": 25,
  "address": {
    "street": "1st Street",
    "state": "Cali"
  },
  "phone": [
    {
      "type": "home",
      "number": "123 456 7890"
    },
    {
      "type": "office",
      "number": "123 456 7890"
    }
  ],
  "children": [],
  "spouse": null
}

Difference Between JavaScript Syntax vs JSON Syntax

JSON syntax is more strict than JavaScript object/array syntax.

Key must be double quoted

// good
{"h":2}

// bad
// {h:2}

// bad
// {'h':2}

Does not allow extra comma at end

// good
{"h":2}

// bad
// {"h":2,}

Does not allow semicolon at the end

// good
{"h":2}

// bad
// {"h":2};

Does not allow undefined

JSON value does not allow undefined. (but null is ok)

// good
{"h":null}

// bad
// {"h":undefined}

Does not allow comment

JSON does not allow comment in JSON string or file. Some language may support it, some not.

JSON