Introducing JSON

In this topic, you learn about JSON files and methods used for manipulating this data.

JSON topics

What's covered in this guide:

  • Get started with JSON files
  • Use methods to manipulate JSON data
  • Use tools for creating JSON text
  • Validate JSON data

Starting with JSON

The JavaScript Object Notation (JSON) file format is a text-based, open standard format which is used to serialize and transmit structured data between a server and web application. The JSON format is easy for humans to read and write. It is also easy for machines to parse and generate. Although it is based on a subset of the JavaScript programming language, it is completely language independent. The JSON format is smaller, faster and easier to parse than XML. Because of these properties, the JSON format is the ideal data-interchange language.

Data types in the JSON format include:

  • Number - Double precision floating point in JavaScript
  • String - Double quoted Unicode with backslash escaping
  • Boolean - true or false
  • Array - An ordered sequence of comma-separated values enclosed in square brackets
  • Object - An unordered collection of key:value pairs, with the colon ":" separating the key and value. It is a comma-separated list enclosed in curly braces.
  • null - null value

The following example describes a person's contact information in JSON format:

{
    "firstName": "John",
    "lastName": "Smith",
    "age": 25,
    "address": {
        "streetAddress": "21 2nd Street",
        "city": "New York",
        "state": "NY",
        "postalCode": 10021
    },
    "phoneNumbers": [
        {
            "type": "home",
            "number": "212 555-1234"
        },
        {
            "type": "fax",
            "number": "646 555-4567"
        }
    ]
}

To learn more about the JSON format, refer to the Introducing JSON and the JSON in JavaScript documents.

Using JSON methods

Since the JSON format is very closely related to the JavaScript language (the characters U+2028 and U+2029 are handled differently in the two languages), it can be used in the language with little effort. The JSON text format uses the same syntax as the code used to create a JavaScript object. The JSON object contains methods for converting values to JSON format and for converting JSON format to values.

The JSON object has several methods which include:

  • The parse() method for converting a JSON string to a JavaScript object or array.
    var jsonstring = '{"firstName":"John","lastName":"Smith","phone":["555-0100","555-0120"]}';
    var contact = JSON.parse(jsonstring);
    console.log(contact.lastName + ", " + contact.firstName);
    //Output: Smith, John

    For details, refer to the JSON.parse Function (JavaScript) document.

  • The stringify() method for converting a JavaScript value, usually an object or array, to a JSON string.

    Example 1:

    var contact = new Object();
    contact.firstName = "John";
    contact.lastName = "Smith";
    contact.phone = ["555-0100","555-0120"];
    var jsonstring = JSON.stringify(contact);
    console.log(jsonstring);
    //Output: { "firstName":"John","lastName":"Smith","phone":["555-0100","555-0120"] }

    Example 2: (Notice that escape characters are used for internal strings)

    var foo = new Object();
    foo.template = '<p class="bar">Hello</p>';
    var jsonstring = JSON.stringify(foo);
    console.log(jsonstring);
    //Output: {"template":"<p class=\"bar\">Hello</p>"}

    For details, refer to the JSON.stringify Function (JavaScript) document.

Create JSON text

If you are using and creating a lot of JSON formatted text files, you can use a JSON editor to help view, edit and format them.

Here are some downloadable JSON editors:

There are also online JSON editors:

There are plenty of free tools to help you work with JSON formatted files. Use your browser to search for other applications.

Validate JSON data

The JSON schema defines the structure of JSON data and is used to validate your JSON data to ensure that is conforms to the standards defined by json.org. There are online tools to validate your JSON files.

Here are some JSON validators: