Functions to parse and serialize JSON

See the dojo/json reference documentation for more information.



Defined by dojo/json

Parses a JSON string to return a JavaScript object.

This function follows native JSON API Throws for invalid JSON strings. This delegates to eval() if native JSON support is not available. By default this will evaluate any valid JS expression. With the strict parameter set to true, the parser will ensure that only valid JSON strings are parsed (otherwise throwing an error). Without the strict parameter, the content passed to this method must come from a trusted source.

Parameter Type Description
str undefined

a string literal of a JSON item, for instance: '{ "foo": [ "bar", 1, { "baz": "thud" } ] }'

strict undefined

When set to true, this will ensure that only valid, secure JSON is ever parsed. Make sure this is set to true for untrusted content. Note that on browsers/engines without native JSON support, setting this to true will run slower.


Defined by dojo/json

Returns a JSON serialization of an object.

Returns a JSON serialization of an object. This function follows native JSON API Note that this doesn't check for infinite recursion, so don't do that!

Parameter Type Description
value undefined

A value to be serialized.

replacer undefined

A replacer function that is called for each value and can return a replacement

spacer undefined

A spacer string to be used for pretty printing of JSON


Example 1

simple serialization of a trivial object

define(["dojo/json"], function(JSON){
    var jsonStr = JSON.stringify({ howdy: "stranger!", isStrange: true });
    doh.is('{"howdy":"stranger!","isStrange":true}', jsonStr);

© 2005–2017 JS Foundation
Licensed under the AFL 2.1 and BSD 3-Clause licenses.