{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":1053,"path":"api-reference/server-api-reference/c_JSONAPI.md","title":"JSON - Global","bundle":null,"doc_type":null,"release":"australia","body":"\n# JSON- Global\n\nThe JSON script include provides methods to create JSON objects from a string, and to turn JSON objects into strings.\n\nThis 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.\n\nThe 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.\n\nThis example creates a JSON object.\n\n```\n// obj is a JSON formatted object\nvar parser = new JSON(); // encode() and decode() are dynamic methods.\nvar str = parser.encode(obj);\n```\n\nThe encode\\(\\) and decode\\(\\) methods are deprecated. Use the JavaScript JSON object instead.\n\nThis example creates a JSON object in a scoped script.\n\n```\n// obj is a JSON formatted object\nvar parser = new global.JSON();\nvar str = parser.encode(obj);\n\n```\n\nThis example uses the static methods.\n\n```\n// obj is a JSON formatted object\nvar str =  JSON.stringify(obj);\n// str is a string containing content to be turned into a JSON formatted object\nvar obj = JSON.parse(str);\n\n```\n\n**Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md)\n\n## JSON - JSON\\(\\)\n\nCreates an instance of the JSON class.\n\nThis class is deprecated. Use the JavaScript JSON object instead.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n## JSON - decode\\(String str\\)\n\nCreates an object or primitive type from a JSON formatted string.\n\n**Note:** This class is deprecated. Use the JavaScript JSON object instead.\n\n|Name|Type|Description|\n|----|----|-----------|\n|str|String|A JSON formatted string.|\n\n|Type|Description|\n|----|-----------|\n|Object|An object created from the specified string.|\n\n```\nvar str = '{\"name\":\"George\",\"lastname\":\"Washington\"}';\nvar parser = new JSON();\nvar obj = parser.decode(str);\ngs.info('The first name is '  + obj.name);\n```\n\nOutput: The first name is George\n\nTo use in a scoped script.\n\n```\nvar str = '{\"name\":\"George\",\"lastname\":\"Washington\"}';\nvar parser = new global.JSON();\nvar obj = parser.decode(str);\ngs.info('The first name is '  + obj.name);\n```\n\nOutput: The first name is George\n\n## JSON - encode\\(Object jsonObject\\)\n\nCreates a string from a JSON object.\n\n**Note:** This class is deprecated. Use the JavaScript JSON object instead.\n\n|Name|Type|Description|\n|----|----|-----------|\n|jsonObject|Object|The JSON object to be turned into a string.|\n\n|Type|Description|\n|----|-----------|\n|String|A JSON formatted string.|\n\n```\nvar obj = {\"name\":\"George\",\"lastname\":\"Washington\"};\nvar parser = new JSON();\nvar str = parser.encode(obj);\ngs.info('The object '  + str);\n```\n\nOutput: The object \\{\"lastname\":\"Washington\",\"name\":\"George\"\\}\n\nTo use in a scoped script.\n\n```\nvar obj = {\"name\":\"George\",\"lastname\":\"Washington\"};\nvar parser = new global.JSON();\nvar str = parser.encode(obj);\ngs.info('The object '  + str);\n```\n\n## JSON - parse\\(String str\\)\n\nCreates an object or primitive type from a JSON formatted string.\n\nThe 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.\n\n|Name|Type|Description|\n|----|----|-----------|\n|str|String|A JSON formatted string.|\n\n|Type|Description|\n|----|-----------|\n|Object|An object created from the specified string.|\n\n```\nvar str = '{\"name\":\"George\",\"lastname\":\"Washington\"}';\nvar obj = JSON.parse(str);\ngs.info('The first name is '  + obj.name);\n```\n\nOutput: The first name is George\n\n## JSON - stringify\\(Object jsonObject\\)\n\nCreates a string from a JSON object.\n\nThe 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.\n\nJSON.stringify\\(\\) converts a value to JSON notation using the following guidelines:\n\n-   If the value has a toJSON\\(\\) method, it is responsible for defining the data that is serialized.\n-   Boolean, number, and string objects are converted to the corresponding primitive values during stringification; in accordance with the traditional conversion semantics.\n-   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)`.\n-   All symbol-keyed properties are ignored, even when using a *replacer\\(\\)* function.\n-   Instances of Date implement the toJSON\\(\\) function by returning a string \\(the same as date.toISOString\\(\\)\\), thus they are treated as strings.\n-   The numbers Infinity and NaN, as well as the value null, are all considered null.\n-   For all other object instances, only their enumerable properties are serialized.\n\n|Name|Type|Description|\n|----|----|-----------|\n|jsonObject|Object|JSON object to turn into a string.|\n\n|Type|Description|\n|----|-----------|\n|String|JSON formatted string.|\n\n```\nvar obj = {\"name\":\"George\",\"lastname\":\"Washington\"};\nvar str =  JSON.stringify(obj);\ngs.info('The object '  + str);\n\n```\n\nOutput: The object \\{\"name\":\"George\",\"lastname\":\"Washington\"\\}\n\nIt 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.\n\n```\nfunction replacer(name, val) {\n   // convert GlideElement to string\n   if ( val && val.constructor === GlideElement ) {\n       return val.toString();\n   } else {\n       return val; // return as is\n   }\n};\n\nvar s = global.JSON.stringify(json_obj, replacer);\ngs.info(\"json=\"+s);\n\n```\n\n"}}