---
sourceDocument: Xanadu API Reference
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - en-US

ft:publication_title :

    - Xanadu API Reference

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# JSON - Global

# JSON - Global {#ariaid-title1}

* Release version: Xanadu
* 
* Updated August 1, 2024
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 4 minutes to read

The JSON script include provides methods to create JSON objects from a string, and to turn JSON objects into strings.

This 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 - JSON() {#ariaid-title2}

Creates an instance of the JSON class.
This class is deprecated. Use the JavaScript JSON object instead.
{#r_JSON-JSON__table_qvw_rgs_dw__entry__3}

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 1. Parameters]

{#r_JSON-JSON__table_qvw_rgs_dw}

## JSON - decode(String str) {#ariaid-title3}

Creates an object or primitive type from a JSON formatted string.
Note:  
This class is deprecated. Use the JavaScript JSON object instead.
{#r_JSON-decode_S__table_kxb_vks_dw__entry__3}

| Name | Type | Description |
|-|-|-|
| str | String | A JSON formatted string. |
[Table 2. Parameters]

{#r_JSON-decode_S__table_kxb_vks_dw} {#r_JSON-decode_S__table_lxb_vks_dw__entry__2}

| Type | Description |
|-|-|
| Object | An object created from the specified string. |
[Table 3. Returns]

{#r_JSON-decode_S__table_lxb_vks_dw}  

    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 - encode(Object jsonObject) {#ariaid-title4}

Creates a string from a JSON object.
Note:  
This class is deprecated. Use the JavaScript JSON object instead.
{#r_JSON-encode_O__table_u2m_phs_dw__entry__3}

| Name | Type | Description |
|-|-|-|
| jsonObject | Object | The JSON object to be turned into a string. |
[Table 4. Parameters]

{#r_JSON-encode_O__table_u2m_phs_dw} {#r_JSON-encode_O__table_v2m_phs_dw__entry__2}

| Type | Description |
|-|-|
| String | A JSON formatted string. |
[Table 5. Returns]

{#r_JSON-encode_O__table_v2m_phs_dw}  

    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 - parse(String str) {#ariaid-title5}

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.
{#r_JSON-parse_S__table_chm_jxx_dw__entry__3}

| Name | Type | Description |
|-|-|-|
| str | String | A JSON formatted string. |
[Table 6. Parameters]

{#r_JSON-parse_S__table_chm_jxx_dw} {#r_JSON-parse_S__table_dhm_jxx_dw__entry__2}

| Type | Description |
|-|-|
| Object | An object created from the specified string. |
[Table 7. Returns]

{#r_JSON-parse_S__table_dhm_jxx_dw}  

    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) {#ariaid-title6}

Creates a string from a JSON object.
The JSON.stringify() method can only convert numbers, strings, and Java
native objects to strings. It cannot convert user-defined objects to strings, unless those
objects provide a toJSON() method. The call to
current.sys_id() returns a GlideElement object which does not have a
toJSON() method, so the return value for stringify is empty: "{}". 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.  
JSON.stringify() converts a value to JSON notation using the following guidelines:

* If the value has a toJSON() method, it is responsible for defining the data that is serialized.
* Boolean, number, and string objects are converted to the corresponding primitive values during stringification; in accordance with the traditional conversion semantics.
* If a function, undefined, or a symbol is encountered during conversion, it is either omitted (when it is found in an object) or censored to null (when it is found in an array). JSON.stringify() also returns undefined when passing in "pure" values, such as `JSON.stringify(function(){})` or `JSON.stringify(undefined)`.
* All symbol-keyed properties are ignored, even when using a <var class="keyword varname">replacer()</var> function.
* Instances of Date implement the toJSON() function by returning a string (the same as date.toISOString()), thus they are treated as strings.
* The numbers Infinity and NaN, as well as the value null, are all considered null.
* For all other object instances, only their enumerable properties are serialized.
{#r_JSON-stringify_O__ul_wsq_4mf_pfb}
{#r_JSON-stringify_O__table_dh2_jyx_dw__entry__3}

| Name | Type | Description |
|-|-|-|
| jsonObject | Object | JSON object to turn into a string. |
[Table 8. Parameters]

{#r_JSON-stringify_O__table_dh2_jyx_dw} {#r_JSON-stringify_O__table_eh2_jyx_dw__entry__2}

| Type | Description |
|-|-|
| String | JSON formatted string. |
[Table 9. Returns]

{#r_JSON-stringify_O__table_eh2_jyx_dw}  

    var obj = {"name":"George","lastname":"Washington"};
    var str =  JSON.stringify(obj);
    gs.info('The object '  + str);

Output: The object {"name":"George","lastname":"Washington"}  
It is also possible to define a replacer() function and use that in the
stringify() call. This function leverages the
GlideElement.toString() method to provide a string representation of
the GlideElement object.

    function replacer(name, val) {
       // convert GlideElement to string
       if ( val && val.constructor === GlideElement ) {
           return val.toString();
       } else {
           return val; // return as is
       }
    };

    var s = global.JSON.stringify(json_obj, replacer);
    gs.info("json="+s);


