Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Scripting
 JSON.stringify Method
Collapse All/Expand All Collapse All
JScript
JSON.stringify Method (Windows Scripting - JScript)

Updated: August 2009

Serializes a JScript value into JavaScript Object Notation (JSON) text.

JSON.stringify(value [, replacer] [, space])
value

Required. A JScript value, usually an object or array, to be serialized.

replacer

Optional. A function or array that filters and transforms the results.

If replacer is a function, JSON.stringify calls the function, passing in the key and value of each member. The return value is serialized instead of the original value. If the function returns undefined, the member will be excluded from the serialization. The key for the root object is an empty string: "".

If replacer is an array, only members with key values in the array will be serialized. The order of serialization is the same as the order of the keys in the array. The replacer array is ignored when the value argument is also an array.

space

Optional. Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.

If space is omitted, the return-value text is generated without any extra white space.

If space is a number, the return-value text is indented with the specified number of white spaces at each level.

If space is a non-empty string, such as '\t', the return-value text is indented with the characters in the string at each level.

A string that contains the serialized JSON text.

Exception

Condition

Invalid replacer argument

The replacer argument is not a function or an array.

Circular reference in value argument not supported

The value argument contains a circular reference.

If the value that is being serialized has a toJSON method, the JSON.stringify method calls the toJSON method and uses the return value for serialization. If the return value of the toJSON method is undefined, the member will not be serialized. This enables an object to determine its own JSON representation.

Values that do not have JSON representations, such as undefined, will not be serialized. In objects, they will be dropped. In arrays, they will be replaced with null.

String values begin and end with a quotation mark. All Unicode characters may be enclosed in the quotation marks except for the characters that must be escaped by using a backslash. The following characters must be preceded by a backslash:

  • Quotation mark (")

  • Backslash (\)

  • Backspace (b)

  • Formfeed (f)

  • Newline (n)

  • Carriage return (r)

  • Horizontal tab (t)

  • Four-hexadecimal-digits (uhhhh)

Order of Execution

During the serialization process, if a toJSON method exists for the value argument, JSON.stringify first calls the toJSON method. If it does not exist, the original value is used. Next, if a replacer argument is provided, the value (original value or toJSON return-value) is replaced with the return-value of the replacer argument. Finally, white spaces are added to the value based on the optional space argument to generate the final serialized JSON text.

This example uses JSON.stringify to serialize the contact object to JSON text. The memberfilter array is defined so that only the surname and phone members are serialized. The firstname member is omitted.

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

var memberfilter = new Array();
memberfilter[0] = "surname";
memberfilter[1] = "phone";
var jsontext = JSON.stringify(contact, memberfilter, "\t");
/* the value of jsontext is
'{
    "surname": "Aaberg",
    "phone": [
        "555-0100",
        "555-0120"
    ]
}'
*/

This example uses JSON.stringify to serialize an array. The replaceToUpper function converts every string in the array to uppercase.

var continents = new Array();
continents[0] = "Europe";
continents[1] = "Asia";
continents[2] = "Australia";
continents[3] = "Antarctica";
continents[4] = "North America";
continents[5] = "South America";
continents[6] = "Africa";

var jsontext = JSON.stringify(continents, replaceToUpper);
/* the value of jsontext is:
'"EUROPE,ASIA,AUSTRALIA,ANTARCTICA,NORTH AMERICA,SOUTH AMERICA,AFRICA"'
*/

function replaceToUpper(key, value) {
    return value.toString().toUpperCase();
}

This example uses the toJSON method to serialize all string member values in Object in uppercase.

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

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

var jsontext = JSON.stringify(contact);

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

Version 5.8

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.

Date

History

Reason

August 2009

Fixed output value comments in examples.

Content bug fix.

August 2008

Added topic.

Information enhancement.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker