What is JSON? Explain JSON.parse() and JSON.stringify() with their common pitfalls.
Answer
JSON (JavaScript Object Notation) is a lightweight data interchange format. Despite the name, it is language-independent and used widely for API communication and configuration.
JSON.stringify(): Converts a JavaScript object into a JSON string
JSON.parse(): Converts a JSON string back into a JavaScript object
Common pitfalls:
undefined, functions, and Symbols are omitted during stringify
NaN and Infinity become null
Date objects become strings (not converted back by parse)
Circular references throw an error
JSON.parse of invalid JSON throws SyntaxError
What a JSON round-trip keeps and drops. Toggle the Date and method under Tweak to see what survives.
stringify flattens to text: Functions and undefined are silently dropped.
Dates become strings: JSON has no Date type; you get an ISO string back.
parse builds new objects: The result is a deep copy of plain data only.
Functions, undefined, and Symbols are silently removed. NaN/Infinity become null. The parse+stringify trick creates a deep clone but loses special types.
Key points
Always wrap JSON.parse() in a try-catch for untrusted input
Use the replacer parameter of stringify to filter or transform values
For deep cloning, prefer structuredClone() over JSON.parse(JSON.stringify())
JSON keys must be double-quoted strings - single quotes are invalid
Common follow-up questions
What is the replacer and reviver parameter in JSON methods?
How would you handle circular references in JSON?
What is the toJSON() method?
Interview tips
Always mention the types that are lost during serialization
The deep clone trick is commonly asked - mention structuredClone as the modern alternative
Knowing about replacer/reviver shows advanced understanding