JSON.parse
Parses a JSON string, constructing the JavaScript value or object described by the string
Syntax
JSON.parse(text, reviver?)Parameters
| Parameter | Type | Description |
|---|---|---|
| text | string | The string to parse as JSON |
| reviver | (key: string, value: any) => any | Optional function that transforms the results |
Return Value
The Object, Array, string, number, boolean, or null value corresponding to the given JSON text
Examples
const json = '{"name":"Alice","age":30}';
const obj = JSON.parse(json);
console.log(obj.name); // 'Alice'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); // truetry {
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
JSON.stringifyConverts a JavaScript value to a JSON string, optionally replacing values or including only specified properties
JSON.rawJSONCreates a raw JSON object that can be included in a JSON string without being re-serialized
JSON.isRawJSONDetermines whether a value is an object returned by JSON.rawJSON
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.