What Is JSON?

JSON Overview

JSON, or JavaScript Object Notation, is a lightweight text format for data interchange. It is easy for people to read and write and is supported by many programming languages.

JSON

  • JSON was popularized by Douglas Crockford.
  • JSON files commonly use the .json extension.
  • The Internet media type is application/json.
  • JSON text exchanged between systems is encoded in UTF-8.

Common Uses

  • Sending structured data between web clients and servers
  • Building web APIs
  • Storing configuration and application data
  • Exchanging data between programs written in different languages

Syntax

JSON values include objects, arrays, strings, numbers, booleans, and null.

Type Description
Number integer or decimal number, optionally with an exponent
String Unicode characters enclosed in double quotation marks
Boolean true or false
Array ordered list of values
Object collection of name-value pairs
null empty value

Numbers

Numbers are written without quotation marks.

{ "marks": 97 }

JSON does not support hexadecimal notation, NaN, or infinity.

Strings

Strings use double quotation marks.

{ "name": "Amit" }

Common escape sequences include \", \\, \/, \b, \f, \n, \r, \t, and \uFFFF.

Booleans and null

{
  "distinction": true,
  "deletedAt": null
}

Arrays

Arrays are ordered lists enclosed in brackets.

{
  "books": [
    { "language": "Java", "edition": "second" },
    { "language": "C++", "edition": "fifth" },
    { "language": "C", "edition": "third" }
  ]
}

Objects

Objects contain unique string keys and values.

{
  "id": "011A",
  "language": "JAVA",
  "price": 500
}

Using JSON in JavaScript

A JSON-shaped JavaScript object can be created with an object literal.

var book = {
  "name": "JSON Guide",
  "price": 500
};

document.write(book.name);

Strict JSON is a data format rather than executable JavaScript. Use JSON.parse() to read JSON text and JSON.stringify() to serialize a JavaScript value.

var text = '{"name":"JSON Guide","price":500}';
var book = JSON.parse(text);
var serialized = JSON.stringify(book);

Example

{
  "book": [
    {
      "id": "01",
      "language": "Java",
      "edition": "third",
      "author": "Herbert Schildt"
    },
    {
      "id": "07",
      "language": "C++",
      "edition": "second",
      "author": "E. Balagurusamy"
    }
  ]
}

References