JSON Parse and JSON Stringify in JavaScript Guide
Learn JSON.parse and JSON.stringify for converting between JavaScript objects and JSON strings. Practical examples of serialization, error handling, and common pitfalls.
JSON.parse and JSON.stringify are the two methods that convert between JavaScript objects and JSON text. JSON.parse reads a JSON string and builds a JavaScript object. JSON.stringify takes a JavaScript object and produces a JSON string.
const jsonString = '{"name":"Maya","score":42}';
const obj = JSON.parse(jsonString);
console.log(obj.name);
const backToString = JSON.stringify(obj);
console.log(backToString);The first log prints "Maya" from the parsed object. The second prints {"name":"Maya","score":42}, identical to the original string. The round trip is lossless for data types that JSON supports.
These two methods are the bridge between your JavaScript code and any external system that speaks JSON. For working with live server responses, see the guide to handling JSON API responses.
JSON.parse -- string to object
JSON.parse takes a JSON-formatted string and returns the equivalent JavaScript value. Numbers stay numbers, booleans stay booleans, arrays become arrays, and objects become objects.
const data = JSON.parse('{"id":1,"active":true,"tags":["js","web"]}');
console.log(data.id);
console.log(data.active);
console.log(data.tags[0]);This prints 1, true, and "js". Every JSON type maps directly to its JavaScript counterpart.
It throws a SyntaxError for invalid input, so always wrap it when parsing data from external sources like API responses or user uploads.
function safeParse(jsonString) {
try {
return JSON.parse(jsonString);
} catch {
return null;
}
}This returns null instead of crashing when the input is malformed. The pattern is essential for production code.
It also accepts an optional reviver function that transforms values during parsing. A common use is restoring Date objects that were serialized as ISO strings.
JSON.stringify -- object to string
JSON.stringify converts a JavaScript value into a JSON string. Objects, arrays, strings, numbers, booleans, and null all survive the conversion.
const user = { name: "Alex", age: 28, active: true };
console.log(JSON.stringify(user));This prints {"name":"Alex","age":28,"active":true}. Object keys become double-quoted strings, and all supported values are converted to their JSON representation.
Not every JavaScript value survives stringification. undefined, functions, and symbols are silently omitted because JSON has no equivalent type. An object with an undefined property will lose that property after a round trip through JSON.
For pretty-printed output, pass a number as the third argument. The value 2 indents each nesting level with two spaces, making nested objects readable. For filtering which keys appear, pass an array of property names as the second argument.
Dates, circular references, and common pitfalls
Date objects become ISO strings during stringification and do not automatically convert back to Date when parsed. The round trip turns a Date into a plain string. Use a reviver function during parsing to restore dates by checking for ISO-formatted strings in known key positions.
Circular references throw a TypeError because JSON is a tree format that cannot represent loops. An object that references itself, directly or through another object, cannot be stringified. If you must serialize circular structures, track visited objects in a custom replacer or use a library.
Using single quotes inside a JSON string passed to JSON.parse causes a SyntaxError. JSON requires double quotes around keys and string values. The outer quotes in your JavaScript code can be single or double, but the JSON content inside must use double quotes.
For formatting dates before serialization, see the guide to formatting dates.
Rune AI
Key Insights
- JSON.parse(jsonString) converts JSON text to a JavaScript object.
- JSON.stringify(value) converts a JavaScript object to a JSON string.
- Always wrap JSON.parse in try-catch for data from external sources.
- JSON.stringify skips undefined, functions, and symbols silently.
- Use JSON.stringify(obj, null, 2) for pretty-printed output.
Frequently Asked Questions
What happens if JSON.parse receives invalid JSON?
Can JSON.stringify handle Date objects?
Why does JSON.stringify skip undefined and functions?
Conclusion
JSON.parse converts a JSON string into a JavaScript object. JSON.stringify converts a JavaScript object into a JSON string. Together they handle nearly all data serialization needs. Remember to wrap parse in try-catch for external data, and use the replacer and space parameters of stringify for filtering and pretty-printing.
More in this topic
Is JavaScript Frontend or Backend? Full Guide
JavaScript is both a frontend and backend language. Learn the difference between browser JavaScript and Node.js, what each is used for, and which one you should learn first.
Learn JavaScript Step by Step Tutorial with Real Examples
Follow a hands-on tutorial that teaches JavaScript by building a real interactive page. Write your first variables, functions, and event listeners with examples you can run.
JavaScript Tutorial: Complete Beginner's Guide to Programming in 2025
A structured learning path for absolute beginners. Learn what to study first, how to practice, and which JavaScript concepts matter most when you are starting from zero.