The JSON.parse() method parses a string as JSON, optionally transforming the value produced by parsing.
Syntax
JSON.parse(text[, reviver])
Parameters
text- The string to parse as JSON. See the
JSONobject for a description of JSON syntax. reviverOptional- If a function, prescribes how the value originally produced by parsing is transformed, before being returned.
Returns
Returns the Object corresponding to the given JSON text.
Throws
Throws a SyntaxError exception if the string to parse is not valid JSON.
Examples
Using JSON.parse()
JSON.parse('{}'); // {}
JSON.parse('true'); // true
JSON.parse('"foo"'); // "foo"
JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
JSON.parse('null'); // null
Using the reviver parameter
If a reviver is specified, the value computed by parsing is transformed before being returned. Specifically, the computed value, and all its properties (beginning with the most nested properties and proceeding to the original value itself), are individually run through the reviver, which is called with the object containing the property being processed as this and with the property name as a string and the property value as arguments. If the reviver function returns undefined (or returns no value, e.g. if execution falls off the end of the function), the property is deleted from the object. Otherwise the property is redefined to be the return value.
The reviver is ultimately called with the empty string and the topmost value to permit transformation of the topmost value. Be certain to handle this case properly, usually by returning the provided value, or JSON.parse() will return undefined.
JSON.parse('{"p": 5}', function(k, v) {
if (k === '') { return v; } // if topmost value, return it,
return v * 2; // else return v * 2.
}); // { p: 10 }
JSON.parse('{"1": 1, "2": 2, "3": {"4": 4, "5": {"6": 6}}}', function(k, v) {
console.log(k); // log the current property name, the last is "".
return v; // return the unchanged property value.
});
// 1
// 2
// 4
// 6
// 5
// 3
// ""
JSON.parse() does not allow trailing commas
// both will throw a SyntaxError
JSON.parse("[1, 2, 3, 4, ]");
JSON.parse("{ \"foo\" : 1, }");
Specifications
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 5.1 (ECMA-262) The definition of 'JSON.parse' in that specification. |
Standard | Initial definition. Implemented in JavaScript 1.7. |
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'JSON.parse' in that specification. |
Standard |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | (Yes) | 3.5 (1.9.1) | 8.0 | 10.5 | 4.0 |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | 1.0 (1.0) | (Yes) | (Yes) | (Yes) |
Gecko-specific notes
Starting Gecko 29 (Firefox 29 / Thunderbird 29 / SeaMonkey 2.26), a malformed JSON string yields a more detailed error message containing the line and column number that caused the parsing error. This is useful when debugging large JSON data.
JSON.parse('[1, 2, 3, 4]');
// SyntaxError: JSON.parse: unexpected character at
// line 1 column 10 of the JSON data