What is JSON.parse() and JSON.stringify() in Node.js?
Why Interviewers Ask This
This is a classic screening question for Node.js roles. Hiring managers ask it early in interviews to gauge your baseline understanding and determine if you can communicate technical concepts clearly.
Answer
JSON.stringify(value, replacer, space) converts a JavaScript value (object, array, primitive) to a JSON string. The optional space parameter adds indentation for readability: JSON.stringify(obj, null, 2). It skips undefined, functions, and symbols. JSON.parse(string) parses a JSON string and returns the corresponding JavaScript value. If the string is invalid JSON, it throws a SyntaxError — always wrap in try/catch when parsing untrusted input: try { const data = JSON.parse(raw); } catch (e) { handle error }. In Express.js, express.json() middleware automatically calls JSON.parse on request bodies and sets req.body. For working with large JSON data efficiently, consider streaming parsers like stream-json rather than parsing the entire payload into memory at once.
Common Mistake
Don't just define the term — demonstrate that you understand when to use it and when not to. Showing awareness of trade-offs is what separates average from strong Node.js candidates.
Previous
What is REST API and how do you build one with Node.js?
Next
What is CORS and how do you enable it in Node.js?