JSON.parse()

Parses a JSON string and returns the corresponding JavaScript value.

Since ES5 Spec ↗

Syntax

JSON.parse(text, reviver)

Parameters

NameTypeRequiredDescription
text string Yes The JSON string to parse. Must be valid JSON.
reviver Function No Function (key, value) called for each pair to transform the parsed result. Return undefined to delete a property.

Returns

any — The JavaScript value (object, array, string, number, boolean, or null).

Throws

  • SyntaxError — the string is not valid JSON.

Examples

const obj = JSON.parse('{"a":1,"b":[2,3]}');
console.log(obj.a, obj.b);
Output
1 [ 2, 3 ]
const o = JSON.parse('{"n":"5"}', (k, v) => k === 'n' ? Number(v) : v);
console.log(o.n);
Output
5

Notes

Throws a SyntaxError on invalid JSON - wrap in try/catch for untrusted input. JSON requires double-quoted keys and strings; trailing commas and comments are not allowed. Dates parse as strings, not Date objects.

Browser & runtime support

EnvironmentSince version
chrome 4
firefox 3.5
safari 4
edge 12
node 0.10

See also