Name

JSON.parse() — parse a JSON-formatted string

Availability

ECMAScript 5

Synopsis

JSON.parse(s)
JSON.parse(s, reviver)

Arguments

s

The string to be parsed

reviver

An optional function that can transform parsed values.

Returns

An object, array, or primitive value parsed from s (and optionally modified by reviver).

Description

JSON.parse() is a global function for parsing JSON-formatted strings. Typically, you pass a single string argument and JSON.parse() returns the JavaScript value that the string represents.

You can use the optional reviver argument to filter or post-process the parsed value before it is returned. If it is specified, the reviver function is invoked once for each primitive value (but not the objects or arrays that contain those primitive values) parsed from s. reviver is invoked with two arguments. The first is a property name—either an object property name or an array index converted to a string. The second argument is the primitive value of that object property or array element. reviver is invoked as a method of the object or array that contains the primitive value. As a special case, if the string s represents a primitive value rather than the more typical object or array, then that primitive value will be stored in a newly-created object using a property whose name is the empty string. In this case, reviver will be invoked once on that newly created object, with an empty string as its first argument and the primitive value as its second.

The return value of the reviver ...

Get JavaScript: The Definitive Guide, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.