What is JSON.parse() and JSON.stringify() in Node.js?
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.
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?