JSON.parse Method (Windows Scripting - JScript)

 

Deserializes JavaScript Object Notation (JSON) text to produce a JScript value.

Syntax

JSON.parse(text [, reviver])

Arguments

  • text
    Required. Valid JSON text.

  • reviver
    Optional. A function that filters and transforms the results. The deserialized object is traversed recursively, and the reviver function is called for each member of the object in post-order (every object is revived after all its members have been revived). For each member, the following occurs:

    • If reviver returns a valid value, the member value is replaced with the value returned by reviver.

    • If reviver returns what it received, the structure is not modified.

    • If reviver returns null or undefined, the object member is deleted.

    The reviver argument is often used to transform JSON representation of International Organization for Standardization (ISO) date strings into Coordinated Universal Time (UTC) format Date objects.

Return Value

A JScript value—an object or array.

Exceptions

Exception

Condition

Exception

Condition

JScript parser errors

The input text does not comply with JSON syntax. To correct the error, do one of the following:

  • Modify the text argument to comply with JSON syntax. For more information, see the BNF syntax notation of JSON objects.

  • Make sure that the text argument was serialized by a JSON-compliant implementation, such as, JSON.stringify.

This example uses JSON.parse to deserialize JSON text into the contact object.

var jsontext = '{"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]}';
var contact = JSON.parse(jsontext);
var fullname = contact.surname + ", " + contact.firstname;
// The value of fullname is "Aaberg, Jesper"

This example uses JSON.parse to deserialize an ISO-formatted date string. The dateReviver function returns Date objects for members that are formatted like ISO date strings.

var jsontext = '{ "hiredate": "2008-01-01T12:00:00Z", "birthdate": "2008-12-25T12:00:00Z" }';
var dates = JSON.parse(jsontext, dateReviver);
var string = dates.birthdate.toUTCString();
// The value of string is "Thu, 25 Dec 2008 12:00:00 UTC"

function dateReviver(key, value) {
    var a;
    if (typeof value === 'string') {
        a = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
        if (a) {
            return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],
                            +a[5], +a[6]));
        }
    }
    return value;
};

Requirements

Version 5.8

Note

Starting with JScript 5.8, by default, the JScript scripting engine supports the language feature set as it existed in version 5.7. This is to maintain compatibility with the earlier versions of the engine. To use the complete language feature set of version 5.8, the Windows Script interface host has to invoke IActiveScriptProperty::SetProperty.

Internet Explorer 8 opts into the JScript 5.8 language features when the document mode for Internet Explorer 8 is "Internet Explorer 8 Standards" mode. For other document modes, Internet Explorer uses the version 5.7 feature set.

JScript 5.8 includes native JavaScript Object Notation (JSON) support and the accessor methods for Document Object Model (DOM) prototypes.

Internet Explorer 8 Release Change

After the initial release of Internet Explorer 8, an update was made to the JSON feature. The update improved JSON interoperability in conformance with the ECMAScript 5 standard. For more information about the update, see KB976662.

Before the Internet Explorer 8 update, a stack overflow exception could occur when JSON.parse used a reviver function and Object.prototype or Array.prototype contained a user-defined property. The following example illustrates these conditions. After the Internet Explorer 8 update, the exception does not occur.

Object.prototype.p1 = {};
var myObj = '[1, 2]';
function reviver(k, v) { return v; }
var obj = JSON.parse(myObj, reviver);

Change History

Date

History

Reason

February 2010

Added information about update to Internet Explorer 8.

Information enhancement.

August 2008

Added topic.

Information enhancement.

See Also

JSON.stringify Method
toJSON Method
JSON Object
JScript Methods