JSON.stringify Guide: Rules & Features
JSON.stringify() is a built-in function used to convert a JavaScript object into a JSON string. Its features and conversion rules are as follows:
- The JSON.stringify() method converts a JavaScript object into a JSON string and returns the result.
- During the conversion process, undefined, functions, and symbol values will be ignored.
- If the property value is a function, undefined, or symbol, it will be ignored during the serialization process.
- An error will be thrown if the object contains circular references (i.e. objects referring to each other).
- When dealing with objects that contain circular references, you can handle the situation of circular references by passing a function as the second parameter.
- You can control the indentation and formatting of the JSON string by passing a third parameter.
- For a Date object, it will be converted into a date string in ISO format.
- For the RegExp object, it will be converted into an empty object.
- Errors will be thrown for other built-in objects such as Map, Set, BigInt, Symbol, and Undefined.