JSON
JSON is a text format for exchanging data. Servers send their responses in it, settings are stored in it, and browsers save data with it. JSON looks a lot like JavaScript objects and arrays, but it is always a string.
What JSON looks like
JSON writes objects in curly braces and arrays in square brackets. The rules are stricter than in JavaScript: keys and strings use double quotes only, and a trailing comma is not allowed.
{
"name": "Anna",
"age": 25,
"tags": ["admin", "editor"]
}JSON can hold strings, numbers, true, false, null, arrays and objects. It has no functions and no undefined.
JSON.stringify — object to string
JSON.stringify() turns a JavaScript value into a JSON string. This is called serialization:
const user = { name: 'Anna', age: 25 };
console.log(JSON.stringify(user)); // {"name":"Anna","age":25}Such a string can be sent to a server or saved, and restored later.
JSON.parse — string to object
JSON.parse() does the opposite: it reads a JSON string and returns an ordinary object you can work with:
const text = '{"name":"Oleg","age":30}';
const user = JSON.parse(text);
console.log(user.age + 1); // 31Readable output
The third argument of JSON.stringify() sets the indentation. With it the JSON is split into lines and is easier for a person to read:
const product = { title: 'Tea', price: 5 };
console.log(JSON.stringify(product, null, 2));
// {
// "title": "Tea",
// "price": 5
// }The second argument, null, is for selecting properties — it is rarely used.
What gets lost
Not everything survives the trip to JSON. Functions and properties set to undefined disappear, and a date becomes a string:
const data = {
name: 'Anna',
greet() {},
phone: undefined,
created: new Date(Date.UTC(2026, 0, 15)),
};
console.log(JSON.stringify(data)); // {"name":"Anna","created":"2026-01-15T00:00:00.000Z"}After JSON.parse() the created field will be a string, not a date.
Common mistake
JSON.parse() throws a SyntaxError when the string is not valid JSON. Single quotes or a trailing comma are the usual culprits:
try {
JSON.parse("{'name': 'Anna'}");
} catch (error) {
console.log(error.name); // SyntaxError
}Always parse data that comes from outside inside try...catch.