What is the use of JSON.parse() in JavaScript?

JavaScript Object Notation (JSON) is a lightweight data interchange format that is easy to read and write for both humans and machines.

JSON.parse() is a built-in method in JavaScript that is used to parse a JSON string and convert it into a JavaScript object.

The syntax of JSON.parse() is as follows:

JSON.parse(text[, reviver])

Here, text is the JSON string that needs to be parsed, and reviver is an optional parameter that can be used to transform the parsed object before it is returned. The return value of JSON.parse() is a JavaScript object that corresponds to the parsed JSON string.

JSON.parse() is often used to retrieve data from a server that is sent in JSON format. The JSON data is received as a string, and JSON.parse() is used to convert it into a JavaScript object that can be easily manipulated and used in the client-side code.

For example, suppose we have the following JSON data:

const jsonData = '{ "name": "John", "age": 30, "city": "New York" }';

To parse this JSON string and convert it into a JavaScript object, we can use JSON.parse() as follows:

const data = JSON.parse(jsonData);

After executing this code, the data variable will contain a JavaScript object with the following properties:

{
  name: "John",
  age: 30,
  city: "New York"
}

We can now access the properties of this object using dot notation or bracket notation, just like we would with any other JavaScript object.

JSON.parse() can also be used to parse more complex JSON data structures, such as arrays and nested objects. For example, suppose we have the following JSON data:

const jsonData =
  '{ "name": "John", "age": 30, "address": { "street": "123 Main St", "city": "New York", "state": "NY" }, "hobbies": ["reading", "traveling", "playing guitar"] }';

To parse this JSON string and convert it into a JavaScript object, we can use JSON.parse() as follows:

const data = JSON.parse(jsonData);

After executing this code, the data variable will contain a JavaScript object with the following properties:

{
  name: "John",
  age: 30,
  address: {
    street: "123 Main St",
    city: "New York",
    state: "NY"
  },
  hobbies: ["reading", "traveling", "playing guitar"]
}

We can now access the properties of the nested object and array using dot notation and bracket notation, respectively.

The reviver parameter in JSON.parse() can be used to transform the parsed object before it is returned. The reviver function is called for each key-value pair in the parsed object, and can be used to modify the value or delete the key-value pair from the object.

The syntax of the reviver function is as follows:

function reviver(key, value) {
  // modify the value or delete the key-value pair
  return value;
}

Here, key is the key of the current key-value pair being processed, and value is the corresponding value. The reviver function should return the modified value, or undefined if the key-value pair should be deleted.

For example, suppose we have the following JSON data:

const jsonData =
  '{ "name": "John", "age": 30, "address": { "street": "123 Main St", "city": "New York", "state": "NY" }, "hobbies": ["reading", "traveling", "playing guitar"] }';

To modify the address property of this object so that it contains only the city and state properties, we can use the following reviver function:

function reviver(key, value) {
  if (key === "address") {
    return {
      city: value.city,
      state: value.state,
    };
  }
  return value;
}

const data = JSON.parse(jsonData, reviver);

After executing this code, the data variable will contain a JavaScript object with the following properties:

{
  name: "John",
  age: 30,
  address: {
    city: "New York",
    state: "NY"
  },
  hobbies: ["reading", "traveling", "playing guitar"]
}

As we can see, the address property of the object has been modified to contain only the city and state properties.

In addition to parsing JSON strings, JSON.parse() can also be used to parse JSON objects. For example, suppose we have the following JSON object:

const jsonObject = { name: "John", age: 30, city: "New York" };

To convert this JSON object into a JavaScript object, we can use JSON.stringify() to convert it into a JSON string, and then JSON.parse() to parse the JSON string into a JavaScript object:

const jsonString = JSON.stringify(jsonObject);
const data = JSON.parse(jsonString);

After executing this code, the data variable will contain a JavaScript object with the same properties as the original JSON object:

{
  name: "John",
  age: 30,
  city: "New York"
}

Thank you for reading, and let’s have conversation with each other

Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.

Read more: