JSON - Global
-
- UpdatedJan 30, 2025
- 4 minutes to read
- Yokohama
- API reference
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.
The encode() and decode() methods are deprecated. Use the JavaScript JSON object instead.
JSON - JSON()
Creates an instance of the JSON class.
This class is deprecated. Use the JavaScript JSON object instead.
Name | Type | Description |
---|---|---|
None |
JSON - decode(String str)
Creates an object or primitive type from a JSON formatted string.
Name | Type | Description |
---|---|---|
str | String | A JSON formatted string. |
Type | Description |
---|---|
Object | An object created from the specified string. |
Example
Output: The first name is George
Example
To use in a scoped script.
Output: The first name is George
JSON - encode(Object jsonObject)
Creates a string from a JSON object.
Name | Type | Description |
---|---|---|
jsonObject | Object | The JSON object to be turned into a string. |
Type | Description |
---|---|
String | A JSON formatted string. |
Example
Output: The object {"lastname":"Washington","name":"George"}
Example
To use in a scoped script.
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.
Name | Type | Description |
---|---|---|
str | String | A JSON formatted string. |
Type | Description |
---|---|
Object | An object created from the specified string. |
Example
Output: The first name is George
JSON - stringify(Object jsonObject)
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.
- 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(){})
orJSON.stringify(undefined)
. - All symbol-keyed properties are ignored, even when using a replacer() 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.
Name | Type | Description |
---|---|---|
jsonObject | Object | JSON object to turn into a string. |
Type | Description |
---|---|
String | JSON formatted string. |
Example
Output: The object {"name":"George","lastname":"Washington"}
Example
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.