JSON.parse()

  • Revision slug: JavaScript/Reference/Global_Objects/JSON/parse
  • Revision title: parse
  • Revision id: 74833
  • Created:
  • Creator: evilpie
  • Is current revision? No
  • Comment 5 words added, 9 words removed

Revision Content

Summary

Parse a string as JSON, optionally transforming the value produced by parsing.

Method of JSON
Implemented in JavaScript 1.7
ECMAScript Edition ECMAScript 5th Edition

Syntax

var v = JSON.parse(text[, reviver]);

Parameters

text 
The string to parse as JSON.  See the JSON object for a description of JSON syntax.
reviver 
If a function, prescribes how the value originally produced by parsing is transformed, before being returned.

Description

JSON.parse parses a string as JSON and returns the parsed value.

JSON.parse('{}'); // {}
JSON.parse('true'); // true
JSON.parse('"foo"); // "foo"
JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
JSON.parse('null'); // null

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.

var transformed =
  JSON.parse('{"p": 5}', function(k, v) { if (k === "") return v; return v * 2; });
// transformed is { p: 10 }

Revision Source

<h3 name="Summary">Summary</h3>
<p>Parse a string as JSON, optionally transforming the value produced by parsing.</p>
<table class="standard-table"> <thead> <tr> <th class="header" colspan="2">Method of <a href="/en/JavaScript/Reference/Global_Objects/JSON"><code>JSON</code></a></th> </tr> </thead> <tbody> <tr> <td>Implemented in</td> <td>JavaScript 1.7</td> </tr> <tr> <td>ECMAScript Edition</td> <td>ECMAScript 5th Edition</td> </tr> </tbody>
</table> <h3 name="Syntax">Syntax</h3>
<pre class="eval">var v = JSON.parse(<em>text</em>[, <em>reviver</em>]);
</pre>
<h3 name="Parameters">Parameters</h3>
<dl> <dt><code>text</code> </dt> <dd>The string to parse as JSON.  See the <a href="/en/JavaScript/Reference/Global_Objects/JSON" title="en/JavaScript/Reference/Global Objects/JSON">JSON</a> object for a description of JSON syntax.</dd>
</dl>
<dl> <dt><code>reviver</code> </dt> <dd>If a function, prescribes how the value originally produced by parsing is transformed, before being returned.</dd>
</dl>
<h3>Description</h3>
<p>JSON.parse parses a string as JSON and returns the parsed value.</p>
<pre class="eval">JSON.parse('{}'); // {}
JSON.parse('true'); // true
JSON.parse('"foo"); // "foo"
JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
JSON.parse('null'); // null
</pre>
<p>If a reviver is specified, the value computed by parsing is <em>transformed</em> 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 <code>this</code> and with the property name as a string and the property value as arguments. If the reviver function returns <code>undefined </code>(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.</p>
<p>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 <code>JSON.parse</code> will return <code>undefined</code>.</p>
<pre class="eval">var transformed =
  JSON.parse('{"p": 5}', function(k, v) { if (k === "") return v; return v * 2; });
// transformed is { p: 10 }
</pre>
Revert to this revision