JSON.parse() and JSON.stringify() are JavaScript’s two native functions for working with JSON, and they go in opposite directions: one turns text into an object, the other turns an object into text. Sounds simple, but the details of each one (extra parameters, behavior with special types, common errors) make a real difference in practice.
JSON.parse(): from string to object
// javascript
const text = '{"name": "Ana", "age": 30}';
const obj = JSON.parse(text);
console.log(obj.name); // Ana
console.log(typeof obj); // object
Takes a string in JSON format and returns the matching JavaScript object. If the string isn’t valid JSON, it throws a SyntaxError.
The second parameter: reviver
JSON.parse() accepts an optional function called a “reviver,” which runs for every key/value pair before the final result is returned. It’s useful for transforming values during parsing, for example converting date strings into Date objects.
// javascript
const text = '{"name": "Ana", "createdAt": "2026-01-15T10:00:00.000Z"}';
const obj = JSON.parse(text, (key, value) => {
if (key === "createdAt") {
return new Date(value);
}
return value;
});
console.log(obj.createdAt instanceof Date); // true
JSON.stringify(): from object to string
// javascript
const obj = { name: "Ana", age: 30 };
const text = JSON.stringify(obj);
console.log(text); // '{"name":"Ana","age":30}'
Does the reverse: takes an object (or array, number, string, etc.) and returns its JSON string representation.
The second parameter: replacer
Just like parse, stringify accepts an optional function (or array) to control which keys make it into the result.
// javascript
const obj = { name: "Ana", password: "123456", age: 30 };
// Using an array: only includes the listed keys
const filteredText = JSON.stringify(obj, ["name", "age"]);
console.log(filteredText); // '{"name":"Ana","age":30}'
// Using a function: more control
const filteredText2 = JSON.stringify(obj, (key, value) => {
if (key === "password") return undefined; // removes this key
return value;
});
This is useful, for example, for stripping sensitive fields (passwords, tokens) before sending data to the client or writing it to a log.
The third parameter: indentation
// javascript
const obj = { name: "Ana", age: 30 };
console.log(JSON.stringify(obj, null, 2));
// {
// "name": "Ana",
// "age": 30
// }
The third parameter controls formatting. A number sets how many spaces to use per indentation level; it also accepts a string (for example "\t" to use a tab).
What happens with types that don’t exist in JSON
JSON.stringify() has to deal with JavaScript types that have no direct equivalent in JSON.
// javascript
const obj = {
a: undefined,
b: function() {},
c: Symbol("test"),
d: 42,
};
console.log(JSON.stringify(obj));
// '{"d":42}'
undefined, functions, and Symbol are simply omitted when they’re values of object properties. Inside an array, though, they turn into null instead of being removed.
// javascript
console.log(JSON.stringify([undefined, function() {}, 42]));
// '[null,null,42]'
Watch out for circular references
// javascript
const obj = { name: "Ana" };
obj.selfReference = obj; // circular reference
JSON.stringify(obj); // TypeError: Converting circular structure to JSON
If an object references itself (directly or indirectly), JSON.stringify() throws an error. This is common in more complex data structures, like tree nodes that keep a reference back to their parent.
Quick comparison
| JSON.parse() | JSON.stringify() | |
|---|---|---|
| Direction | string to object | object to string |
| Second parameter | reviver (function) | replacer (function or array) |
| Third parameter | doesn’t exist | spacing (number or string) |
| Common error | SyntaxError on invalid JSON | TypeError on circular reference |
Quick checklist
- Do you know when to use
parse(receiving data) versusstringify(sending/saving data)? - Are you using
replacerto strip sensitive fields before exposing data externally? - Watching out for circular references in complex objects before calling
stringify?
If you want to quickly test a JSON before using parse, paste it into JSON Formatter & Validator.