JavaScript JSON Reference

Posted by

Introduction to JavaScript JSON

JavaScript Object Notation (JSON) is a lightweight data-interchange format. It is a text-based, human-readable format for representing simple data structures and associative arrays (called objects). JSON is used to store and exchange data between applications and is often used as an alternative to XML. It is language independent, easy to understand and support, and works well with modern web technologies.

JSON Syntax

JSON syntax is based on two types of data structures: objects and arrays. An object is an unordered list of name-value pairs, where the names and values are strings, numbers, booleans, nulls, objects, or arrays. A JSON object is surrounded by curly braces { } and contains a comma-separated list of name-value pairs.

An array is an ordered list of values surrounded by square brackets [ ]. The values can be strings, numbers, booleans, nulls, objects, or arrays.

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

{
  "name": "John Smith",
  "age": 32,
  "isMarried": true,
  "children": [
    {
      "name": "Sally",
      "age": 5
    },
    {
      "name": "Billy",
      "age": 3
    }
  ]
}

[/dm_code_snippet]

JSON Parsing

JSON parsing is the process of transforming a JSON string into a JavaScript object or array. This is typically done using a JavaScript library such as jQuery or Underscore.js, or a JSON parser such as JSON.parse().

To convert a JSON string into a JavaScript object or array, use the global JSON.parse() method, which returns a JavaScript object or array. For example, the following code parses a JSON string into a JavaScript object:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var jsonString = '{"name": "John Smith", "age": 32}';
var user = JSON.parse(jsonString);

[/dm_code_snippet]

The resulting user object has two properties, name and age, which contain the corresponding values from the JSON string.

JSON Stringify

JSON stringify is the process of transforming a JavaScript object or array into a JSON string. This is typically done using a JavaScript library such as jQuery or Underscore.js, or a JSON stringify method such as JSON.stringify().

To convert a JavaScript object or array into a JSON string, use the global JSON.stringify() method. For example, the following code converts a JavaScript object into a JSON string:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var user = {
  "name": "John Smith",
  "age": 32
};
var jsonString = JSON.stringify(user);

[/dm_code_snippet]

The resulting jsonString variable contains the following JSON string:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

{"name": "John Smith", "age": 32}

[/dm_code_snippet]

Using JSON in JavaScript

JSON can be used to store and exchange data between applications. It is a lightweight and easy to understand format, making it ideal for use with modern web technologies. JSON is also a language-independent data format, and can be used in any programming language that supports the JSON data format.

JSON data can be stored in the browser using the localStorage and sessionStorage objects. Storing data in the browser allows web applications to load quickly and to store data between page visits. For example, the following code stores a JSON string in the browser’s localStorage object:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var user = {
  "name": "John Smith",
  "age": 32
};
localStorage.setItem("user", JSON.stringify(user));

[/dm_code_snippet]

The data can then be retrieved from the localStorage object using the getItem() method:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var userString = localStorage.getItem("user");
var user = JSON.parse(userString);

[/dm_code_snippet]

JSON can also be used to exchange data between applications via AJAX requests. For example, an application can make an AJAX request to an API endpoint, and the API can return a JSON response. The application can then parse the JSON response and use the data to display content on the page.

Conclusion

In this tutorial, we’ve discussed the basics of the JavaScript JSON format. We’ve discussed the syntax of JSON, how to parse and stringify JSON, and how to use JSON to store and exchange data. We hope this tutorial has been helpful in getting you started with using JSON in your JavaScript applications.