Skip to main content

JSON in JavaScript

JSON stands for JavaScript Object Notation. It is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. JSON is a text format that is completely language-independent but uses conventions that are familiar to programmers of the C family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others.

JSON is built on two structures:

  1. A collection of key/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  2. An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

These are universal data structures. Virtually all modern programming languages support them in one form or another. It makes sense that a data format that is interchangeable with programming languages also be based on these structures.

In JSON, they take on these forms:

  1. An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).
  2. An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).
  3. A value can be a string in double quotes, a number, true, false, null, an object, or an array. These structures can be nested.
  4. JSON is often used to exchange data between a server and a web application.

In this tutorial, we will learn how to work with JSON in JavaScript. We will learn how to parse JSON data, stringify JSON data, and how to work with JSON objects and arrays in JavaScript. We will also learn how to convert JSON data to JavaScript objects and arrays and vice versa. We will also learn how to work with JSON data in JavaScript using the JSON object. We will also learn how to handle errors when working with JSON data in JavaScript. We will also learn how to work with JSON data in JavaScript using the fetch API. We will also learn how to work with JSON data in JavaScript using the XMLHttpRequest object. We will also learn how to work with JSON data in JavaScript using the jQuery library. We will also learn how to work with JSON data in JavaScript using the Axios library.

JSON Syntax​

JSON syntax is derived from JavaScript object notation syntax:

  1. Data is in name/value pairs.

    • Data is separated by commas.
    • Curly braces hold objects.
    • Square brackets hold arrays.

    data.json
    {
    "name": "Ajay Dhangar",
    "age": 23,
    "isStudent": true,
    "courses": ["Math", "Science", "English"],
    "address": {
    "street": "Mandsaur, MP",
    "city": "Mandsaur",
    "state": "MP",
    "zip": "458002"
    }
    }
  2. Data is in key/value pairs.

    • Keys must be strings.
    • Values must be valid JSON data types.
    • Each key/value pair is separated by a comma.
    • The key is separated from the value by a colon.

    data.json
    {
    "name": "Ajay Dhangar",
    "age": 23,
    "isStudent": true,
    "courses": ["Math", "Science", "English"],
    "address": {
    "street": "Mandsaur, MP",
    "city": "Mandsaur",
    "state": "MP",
    "zip": "458002"
    }
    }
  3. JSON values can be:

    • A string.
    • A number.
    • An object (JSON object).
    • An array.
    • A boolean.
    • null.

JSON Data Types​

JSON supports the following data types:

  1. String: A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslashes (\) to escape characters.

    data.json
    {
    "name": "Ajay Dhangar"
    }
  2. Number: A number is a signed decimal number that may contain a fractional part and may use exponential E notation.

    data.json
    {
    "age": 23
    }
  3. Object: An object is an unordered collection of key/value pairs.

    data.json
    {
    "address": {
    "street": "Mandsaur, MP",
    "city": "Mandsaur",
    "state": "MP",
    "zip": "458002"
    }
    }
  4. Array: An array is an ordered collection of values.

    data.json
    {
    "courses": ["Math", "Science", "English"]
    }
  5. Boolean: A boolean is either true or false.

    data.json
    {
    "isStudent": true
    }
  6. Null: A null value represents a null value.

    data.json
    {
    "address": null
    }

JSON in JavaScript​

In JavaScript, you can work with JSON data using the following methods:

  1. JSON.parse(): The JSON.parse() method parses a JSON string and returns a JavaScript object.

    JSON.parse() Example
    const jsonString = `{
    "name": "Ajay Dhangar",
    "age": 23,
    "isStudent": true,
    "courses": ["Math", "Science", "English"],
    "address": {
    "street": "Mandsaur, MP",
    "city": "Mandsaur",
    "state": "MP",
    "zip": "458002"
    }
    }`;

    const jsonObject = JSON.parse(jsonString);

    console.log(jsonObject);
  2. JSON.stringify(): The JSON.stringify() method converts a JavaScript object or value to a JSON string.

    JSON.stringify() Example
     const jsonObject = {
    name: "Ajay Dhangar",
    age: 23,
    isStudent: true,
    courses: ["Math", "Science", "English"],
    address: {
    street: "Mandsaur, MP",
    city: "Mandsaur",
    state: "MP",
    zip: "458002"
    }
    };

    const jsonString = JSON.stringify(jsonObject);

    console.log(jsonString); //

  3. JSON Object: The JSON object provides methods for working with JSON data.

    • JSON.parse(): Parses a JSON string and returns a JavaScript object.
    • JSON.stringify(): Converts a JavaScript object or value to a JSON string.

    JSON Object Example
    const jsonString = `{
    "name": "Ajay Dhangar",
    "age": 23,
    "isStudent": true,
    "courses": ["Math", "Science", "English"],
    "address": {
    "street": "Mandsaur, MP",
    "city": "Mandsaur",
    "state": "MP",
    "zip": "458002"
    }
    }`;

    const jsonObject = JSON.parse(jsonString);

    console.log(jsonObject);

    const jsonString = JSON.stringify(jsonObject);

    console.log(jsonString);
  4. Fetch API: The Fetch API provides a JavaScript interface for accessing and manipulating parts of the HTTP pipeline, such as requests and responses.

    Fetch API Example
     fetch('https://jsonplaceholder.typicode.com/posts')
    .then(response => response.json())
    .then(data => console.log(data))
    .catch(error => console.error(error));
  5. XMLHttpRequest Object: The XMLHttpRequest object is used to interact with servers.

    XMLHttpRequest Object Example
    const xhr = new XMLHttpRequest();

    xhr.open('GET', 'https://jsonplaceholder.typicode.com/posts', true);

    xhr.onload = function () {
    if (xhr.status >= 200 && xhr.status < 300) {
    console.log(JSON.parse(xhr.responseText));
    } else {
    console.error('Request failed with status:', xhr.status);
    }
    };

    xhr.send();
  6. jQuery Library: The jQuery library provides methods for working with JSON data.

    jQuery Library Example
    $.getJSON('https://jsonplaceholder.typicode.com/posts', function(data) {
    console.log(data);
    });
  7. Axios Library: The Axios library provides methods for working with JSON data.

    Axios Library Example
    axios.get('https://jsonplaceholder.typicode.com/posts')
    .then(response => console.log(response.data))
    .catch(error => console.error(error));

JSON Error Handling​

When working with JSON data in JavaScript, you should handle errors to prevent your application from crashing. You can handle errors using the try...catch statement.

Error Handling Example
try {
const jsonString = `{
"name": "Ajay Dhangar",
"age": 23,
"isStudent": true,
"courses": ["Math", "Science", "English"],
"address": {
"street": "Mandsaur, MP",
"city": "Mandsaur",
"state": "MP",
"zip": "458002"
}
}`;

const jsonObject = JSON.parse(jsonString);

console.log(jsonObject);
} catch (error) {
console.error(error);
}

In the above example, we are using the try...catch statement to handle errors when parsing JSON data. If an error occurs while parsing JSON data, the error will be caught and logged to the console.

Note

You should always handle errors when working with JSON data in JavaScript to prevent your application from crashing.

Conclusion​

In this tutorial, we learned about JSON in JavaScript. We learned how to parse JSON data, stringify JSON data, and how to work with JSON objects and arrays in JavaScript. We also learned how to convert JSON data to JavaScript objects and arrays and vice versa. We also learned how to work with JSON data in JavaScript using the JSON object. We also learned how to handle errors when working with JSON data in JavaScript. We also learned how to work with JSON data in JavaScript using the fetch API. We also learned how to work with JSON data in JavaScript using the XMLHttpRequest object. We also learned how to work with JSON data in JavaScript using the jQuery library. We also learned how to work with JSON data in JavaScript using the Axios library. JSON is a powerful data interchange format that is widely used in web development. It is easy to read and write and easy to parse and generate. JSON is a text format that is completely language-independent but uses conventions that are familiar to programmers of the C family of languages. JSON is built on two structures: A collection of key/value pairs and an ordered list of values. JSON is often used to exchange data between a server and a web application. It is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. JSON is a universal data structure that is supported by virtually all modern programming languages.