toJSON Method (Windows Scripting - JScript)

 

Used by the JSON.stringify method to enable the transformation of an object's data of before the JavaScript Object Notation (JSON) serialization.

objectname.toJSON()

objectname

Required. An object for which JSON serialization is wanted.

The toJSON method is used by the JSON.stringify method. JSON.stringify serializes a JScript value into JSON text. If a toJSON method is provided to JSON.stringify, the toJSON method is called when JSON.stringify is called.

The toJSON method is a built-in member of the Date JScript object. It returns an ISO-formatted date string for the UTC time zone (denoted by the suffix Z).

You can override the toJSON method for the Date type, or define a toJSON method for other object types to achieve transformation of data for the specific object type before JSON serialization.

The following example uses the toJSON method to serialize string member values in uppercase. The toJSON method is called when JSON.stringify is called.

var contact = new Object();
contact.firstname = "Jesper";
contact.surname = "Aaberg";
contact.phone = ["555-0100", "555-0120"];

contact.toJSON = function(key)
 {
    var replacement = new Object();
    for (var val in this)
    {
        if (typeof (this[val]) === 'string')
            replacement[val] = this[val].toUpperCase();
        else
            replacement[val] = this[val]
    }
    return replacement;
};

var jsonText = JSON.stringify(contact);

/* The value of jsonText is:
'{"firstname":"JESPER","surname":"AABERG","phone":["555-0100","555-0120"]}'
*/

The following example illustrates how to use the toJSON method that is a built-in member of the Date object.

var dt = new Date('8/24/2009');
dt.setUTCHours(7, 30, 0);
var jsonText = JSON.stringify(dt);

/* The value of jsonText is:
'"2009-08-24T07:30:00Z"'
*/

Requirements

Version 5.8

System_CAPS_noteNote

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.

System_CAPS_noteNote

The initial release of Internet Explorer 8 included built-in toJSON methods for the Number, String, and Boolean objects. Those toJSON methods returned the same result as the built-in toString method for those objects. Internet Explorer 8 JSON support was subsequently updated to comply with the ECMAScript 5 standard. The Number, String, and Boolean methods for the toJSON method were removed. For more information about the update, see KB976662.

Applies To: Date Object

Change History

Date

History

Reason

February 2010

Added example.

Information enhancement.

February 2010

Added note about update to Internet Explorer 8.

Information enhancement.

August 2008

Added topic.

Information enhancement.

Show: