JSON

JSON.parse

Parses a JSON string, constructing the JavaScript value or object described by the string

Syntax

JavaScript
JSON.parse(text, reviver?)

Parameters

ParameterTypeDescription
textstringThe string to parse as JSON
reviver(key: string, value: any) => anyOptional function that transforms the results

Return Value

The Object, Array, string, number, boolean, or null value corresponding to the given JSON text

Examples

Basic Usage
const json = '{"name":"Alice","age":30}';
const obj = JSON.parse(json);
console.log(obj.name); // 'Alice'
Practical Example
const json = '{"date":"2024-01-15T00:00:00.000Z"}';
const obj = JSON.parse(json, (key, value) => {
  if (key === 'date') return new Date(value);
  return value;
});
console.log(obj.date instanceof Date); // true
Advanced Usage
try {
  JSON.parse('invalid json');
} catch (err) {
  console.log(err instanceof SyntaxError); // true
}

Understanding JSON.parse

The JSON.parse method in JavaScript parses a JSON string, constructing the JavaScript value or object described by the string. It belongs to the JSON object and is one of the most widely used methods for working with json values in modern JavaScript and TypeScript applications.

The method signature is JSON.parse(text, reviver?). It accepts 2 parameters: text, reviver. When called, it returns the object, array, string, number, boolean, or null value corresponding to the given json text. Understanding when and how to use parse() helps you write more expressive, readable code.

Common use cases for JSON.parse include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like json-stringify, json-rawjson, json-israwjson, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for JSON.parse is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More JSON Methods

Other methods in the JSON object

Related Tools

More JSON Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.