JavaScript JSON.parse()
Syntax & Examples

JSON.parse() static-method

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.


Syntax of JSON.parse()

There are 2 variations for the syntax of JSON.parse() static-method. They are:

1.
JSON.parse(text)

Parameters

ParameterOptional/RequiredDescription
textrequiredThe string to parse as JSON. This must be a valid JSON string.

This static-method parses the JSON string text and returns the resulting JavaScript value.

Returns value of type Object.

2.
JSON.parse(text, reviver)

Parameters

ParameterOptional/RequiredDescription
textrequiredThe string to parse as JSON. This must be a valid JSON string.
reviveroptionalA function that transforms the results. This function is called for each member of the object.

This static-method parses the JSON string text and uses the reviver function to transform the parsed value before returning it.

Returns value of type Object.



✐ Examples

1 Parsing a simple JSON string

In JavaScript, we can use the JSON.parse() method to parse a simple JSON string into a JavaScript object.

For example,

  1. We define a JSON string jsonString with a key-value pair.
  2. We use the JSON.parse() method to parse jsonString into a JavaScript object.
  3. The parsed object is stored in the variable obj.
  4. We log obj to the console using console.log() method.

JavaScript Program

const jsonString = '{"key": "value"}';
const obj = JSON.parse(jsonString);
console.log(obj);

Output

{ key: 'value' }

2 Parsing JSON string with reviver function

In JavaScript, we can use the JSON.parse() method with a reviver function to transform the parsed values.

For example,

  1. We define a JSON string jsonString with a key-value pair.
  2. We use the JSON.parse() method with a reviver function that transforms the value of the key 'key' to uppercase.
  3. The transformed object is stored in the variable obj.
  4. We log obj to the console using console.log() method.

JavaScript Program

const jsonString = '{"key": "value"}';
const obj = JSON.parse(jsonString, (key, value) => {
  return key === 'key' ? value.toUpperCase() : value;
});
console.log(obj);

Output

{ key: 'VALUE' }

3 Handling invalid JSON string

In JavaScript, the JSON.parse() method will throw an error if the string to parse is not valid JSON.

For example,

  1. We define an invalid JSON string invalidJsonString.
  2. We attempt to parse the invalid JSON string using the JSON.parse() method inside a try-catch block.
  3. An error is caught and logged to the console using console.error() method.

JavaScript Program

const invalidJsonString = '{key: "value"}';
try {
  const obj = JSON.parse(invalidJsonString);
} catch (error) {
  console.error('Invalid JSON:', error);
}

Output

Invalid JSON: SyntaxError: Unexpected token k in JSON at position 1

Summary

In this JavaScript tutorial, we learned about parse() static-method of JSON: the syntax and few working examples with output and detailed explanation for each example.