- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
Explanation:-
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(){})
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.
Example:-
var obj = {"name":"George","lastname":"Washington"};
var str = JSON.stringify(obj);
gs.info('The object ' + str);
Output:-
The object {"name":"George","lastname":"Washington"}
If my content helped you in anyway, please mark this content as BOOKMARK, SUBSCRIBE & HELPFUL
Best Regards,
Prashant Kumar (LearnIT)
YouTube Channel LearnIT: https://www.youtube.com/@learnitwithprashant
Blog LearnIT: https://medium.com/@LearnITbyPrashant
Prashant Kumar LinkedIn: https://www.linkedin.com/in/learnitbyprashant/
ServiceNow Community Prashant Kumar - https://www.servicenow.com/community/user/viewprofilepage/user-id/19635
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.