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.

5 min read

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.

javascriptjavascript
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.

javascriptjavascript
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.

javascriptjavascript
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.

javascriptjavascript
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

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.
RunePowered by Rune AI

Frequently Asked Questions

What happens if JSON.parse receives invalid JSON?

It throws a SyntaxError. Always wrap JSON.parse in a try-catch block when parsing JSON from external sources like API responses or user input.

Can JSON.stringify handle Date objects?

Yes, but it converts dates to ISO strings. When you parse the result, the date becomes a string, not a Date object. Use a reviver function to restore dates during parsing.

Why does JSON.stringify skip undefined and functions?

JSON is a data-only format. undefined, functions, and symbols are not valid JSON types and are silently omitted. Use null instead of undefined for intentional empty values.

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.