JSON.stringify or stringify(Object jsonObject)

Prashant LearnIT
2 min readJan 9, 2024

--

Explanation:-

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.

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 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.

Example:-

var obj = {“name”:”George”,”lastname”:”Washington”};

var str = JSON.stringify(obj);

gs.info(‘The object ‘ + str);

Output:-

The object {“name”:”George”,”lastname”:”Washington”}

--

--

Prashant LearnIT

I would like to share my professional and technical knowledge with everyone. I will provide all new information related to ServiceNow and other technical stuff.