Revision 35893 of Using native JSON

  • Revision slug: Using_native_JSON
  • Revision title: Using native JSON
  • Revision id: 35893
  • Created:
  • Creator: RobinEgg
  • Is current revision? No
  • Comment no wording changes; page display name reset to default

Revision Content

{{ gecko_minversion_header("1.9.1") }}

This article covers the ECMAScript 5 compliant native JSON object added in Gecko 1.9.1.  For basic information on using JSON in previous versions of Firefox, see JSON.

The native JSON object includes two key methods.  The JSON.parse() method parses a JSON string, reconstructing the original JavaScript object, while the JSON.stringify() method accepts a JavaScript object and returns its JSON equivalent.

Note: JSON does not support objects that include methods.  Attempting to convert such an object into JSON format will result in a TypeError exception.

Parsing JSON strings

To convert a JSON string into a JavaScript object, you simply pass the JSON into the JSON.parse() method, like this:

var jsObject = JSON.parse(jsonString);

Converting objects into JSON

To convert a JavaScript object into a JSON string, pass the object into the JSON.stringify() method:

var foo = {};
foo.bar = "new property";
foo.baz = 3;

var jsObject = JSON.stringify(foo);

jsObject now holds {"bar":"new property","baz":3}.

See also

 

Revision Source

<p>{{ gecko_minversion_header("1.9.1") }}</p>
<p>This article covers the ECMAScript 5 compliant native JSON object added in Gecko 1.9.1.  For basic information on using JSON in previous versions of Firefox, see <a class="internal" href="/en/JSON" title="En/JSON">JSON</a>.</p>
<p>The native JSON object includes two key methods.  The <code>JSON.parse()</code> method parses a JSON string, reconstructing the original JavaScript object, while the <code>JSON.stringify()</code> method accepts a JavaScript object and returns its JSON equivalent.</p>
<div class="note"><strong>Note:</strong> JSON does not support objects that include methods.  Attempting to convert such an object into JSON format will result in a <code>TypeError</code> exception.</div>
<h2>Parsing JSON strings</h2>
<p>To convert a JSON string into a JavaScript object, you simply pass the JSON into the <code>JSON.parse()</code> method, like this:</p>
<pre>var jsObject = JSON.parse(jsonString);</pre>
<h2>Converting objects into JSON</h2>
<p>To convert a JavaScript object into a JSON string, pass the object into the <code>JSON.stringify()</code> method:</p>
<pre class="eval">var foo = {};
foo.bar = "new property";
foo.baz = 3;

var jsObject = JSON.stringify(foo);
</pre>
<p><code>jsObject</code> now holds <code>{"bar":"new property","baz":3}</code>.</p>
<h2>See also</h2>
<ul> <li><a class="internal" href="/En/JavaScript/ECMAScript_5_support_in_Mozilla" title="En/JavaScript/ECMAScript 5 support in Mozilla">ECMAScript 5 support in Mozilla</a></li> <li><a class="internal" href="/en/JSON" title="En/JSON">JSON</a></li>
</ul>
<p> </p>
Revert to this revision