JSON - Global

Provides methods to create JSON objects from a string, and to turn JSON objects into strings.

The JSON API has dynamic and static methods. You access the dynamic methods by creating a JSON object. To use the dynamic methods in a scoped application, add the global prefix when calling the constructor. You access the static methods by using the static JSON object.

The JavaScript ES5 native JSON object is used instead of the JSON static methods. If your script needs the old behavior, use the encode() and decode() methods.

This example creates a JSON object.
// obj is a JSON formatted object
var parser = new JSON(); // encode() and decode() are dynamic methods.
var str = parser.encode(obj);

The encode() and decode() methods are deprecated. Use the JavaScript JSON object instead.

This example creates a JSON object in a scoped script.
// obj is a JSON formatted object
var parser = new global.JSON();
var str = parser.encode(obj);
This example uses the static methods.
// obj is a JSON formatted object
var str =  JSON.stringify(obj);
// str is a string containing content to be turned into a JSON formatted object
var obj = JSON.parse(str);

JSON - encode(Object jsonObject)

Creates a string from a JSON object.

Note: This class is deprecated. Use the JavaScript JSON object instead.
Table 1. Parameters
Name Type Description
jsonObject Object The JSON object to be turned into a string.
Table 2. Returns
Type Description
String A JSON formatted string.
var obj = {“name":"George","lastname":"Washington"};
var parser = new JSON();
var str = parser.encode(obj);
gs.info(‘The object ’  + str);

Output: The object {"lastname":"Washington","name":"George"}

To use in a scoped script.

var obj = {“name":"George","lastname":"Washington"};
var parser = new global.JSON();
var str = parser.encode(obj);
gs.info(‘The object ’  + str);

JSON - decode(String str)

Creates an object or primitive type from a JSON formatted string.

Note: This class is deprecated. Use the JavaScript JSON object instead.
Table 3. Parameters
Name Type Description
str String A JSON formatted string.
Table 4. Returns
Type Description
Object An object created from the specified string.
var str = ‘{“name":"George","lastname":"Washington”}’;
var parser = new JSON();
var obj = parser.decode(str);
gs.info(‘The first name is ’  + obj.name);

Output: The first name is George

To use in a scoped script.

var str = ‘{“name":"George","lastname":"Washington”}’;
var parser = new global.JSON();
var obj = parser.decode(str);
gs.info(‘The first name is ’  + obj.name);

Output: The first name is George

JSON - JSON()

Creates an instance of the JSON class.

This class is deprecated. Use the JavaScript JSON object instead.

Table 5. Parameters
Name Type Description
None

JSON - parse(String str)

Creates an object or primitive type from a JSON formatted string.

The JavaScript ES5 native JSON object is used instead of the JSON static methods. If your script needs the old behavior, use the encode() and decode() methods.

Table 6. Parameters
Name Type Description
str String A JSON formatted string.
Table 7. Returns
Type Description
Object An object created from the specified string.
var str = ‘{“name":"George","lastname":"Washington”}’;
var obj = JSON.parse(str);
gs.info(‘The first name is ’  + obj.name);

Output: The first name is George

JSON - stringify(Object jsonObject)

Creates a string from a JSON object.

The JavaScript ES5 native JSON object is used instead of the JSON static methods. If your script needs the old behavior, use the encode() and decode() methods.

Table 8. Parameters
Name Type Description
jsonObject Object The JSON object to be turned into a string.
Table 9. Returns
Type Description
String A JSON formatted string.
var obj = {“name":"George","lastname":"Washington"};
var str =  JSON.stringify(obj);
gs.info(‘The object ’  + str);

Output: The object {"name":"George","lastname":"Washington"}