JavaScript JSON.parse(text, reviver


The JSON.parse(text, reviver) method in JavaScript is used to parse a JSON string and convert it into a JavaScript value or object. This method is part of the JSON object, which provides utilities for working with JSON data.

Syntax:

JSON.parse(text, reviver);

Parameters:

  1. text: A valid JSON string that you want to parse into a JavaScript value. If the string is not valid JSON, a SyntaxError will be thrown.
  2. reviver (optional): A function that can be used to transform the parsed values. It receives two arguments: the key and the value of each property in the parsed object. You can return a modified value or undefined to omit the property from the resulting object.

Return Value:

  • Returns the JavaScript value or object that the JSON string represents.

Example 1: Basic Usage

const jsonString = '{"name":"Alice","age":30,"hobbies":["reading","traveling"]}'; const obj = JSON.parse(jsonString); console.log(obj); // Output: { name: 'Alice', age: 30, hobbies: [ 'reading', 'traveling' ] } console.log(obj.name); // Output: 'Alice'

In this example, a JSON string is parsed into a JavaScript object.

Example 2: Using a Reviver Function

The reviver function allows you to transform the values during the parsing process.

const jsonString = '{"name":"Alice","age":"30","birthYear":1992}'; // Reviver function to convert age from string to number const obj = JSON.parse(jsonString, (key, value) => { if (key === 'age') { return Number(value); // Convert age to a number } return value; // Return unchanged for other keys }); console.log(obj); // Output: { name: 'Alice', age: 30, birthYear: 1992 } console.log(typeof obj.age); // Output: 'number'

In this example, the reviver function converts the age property from a string to a number.

Example 3: Omitting Properties with Reviver

You can also use the reviver function to omit properties from the resulting object.

const jsonString = '{"name":"Alice","age":30,"password":"secret"}'; // Reviver function to exclude the 'password' property const obj = JSON.parse(jsonString, (key, value) => { if (key === 'password') { return undefined; // Omit this property } return value; }); console.log(obj); // Output: { name: 'Alice', age: 30 }

In this case, the reviver function removes the password property from the resulting object.

Example 4: Handling Nested Objects

The reviver function can be useful for transforming nested objects as well.

const jsonString = '{"name":"Alice","age":30,"address":{"city":"Wonderland","zip":"12345"}}'; const obj = JSON.parse(jsonString, (key, value) => { if (key === 'zip') { return Number(value); // Convert zip to a number } return value; }); console.log(obj); // Output: { name: 'Alice', age: 30, address: { city: 'Wonderland', zip: 12345 } } console.log(typeof obj.address.zip); // Output: 'number'

Here, the reviver function converts the zip code in a nested object to a number.

Summary:

  • JSON.parse(text, reviver) is used to convert a JSON string into a JavaScript value or object.
  • The text parameter must be a valid JSON string; otherwise, it throws a SyntaxError.
  • The optional reviver function allows for transformation of the parsed values, including modifying or omitting properties.
  • This method is essential for deserializing JSON data received from APIs or other sources into usable JavaScript objects.