JSON
JSON (JavaScript Object Notation) is a text format for data. APIs send it, apps save it, and nearly every programming language can read it. JavaScript has two built-in functions for it: JSON.stringify() turns a value into JSON text, and JSON.parse() turns JSON text back into a value.
What JSON looks like
JSON looks like a JavaScript object literal, but with stricter rules:
{
"name": "Amina",
"age": 19,
"courses": ["JavaScript", "CSS"],
"graduated": false,
"mentor": null
}
- Keys must be in double quotes, and so must strings. Single quotes are not allowed.
- Values can only be strings, numbers,
true/false,null, arrays and objects. - No functions, no
undefined, no comments and no trailing comma after the last item.
Because JSON is just text, it can travel over the internet or sit in a file or in localStorage, and be turned back into data at the other end.
stringify and parse convert between them.JSON.stringify: object to text
const student = { name: "Amina", age: 19, courses: ["JS", "CSS"], graduated: false };
const text = JSON.stringify(student);
console.log(text);
console.log(typeof text);
console.log(text.length);
The result is a compact string with no spaces. To make it readable for humans, pass a third argument: the number of spaces to indent by. (The second argument is a "replacer", usually null.)
console.log(JSON.stringify({ county: "Nakuru", code: 32 }, null, 2));
Passing an array of key names as the replacer keeps only those keys:
console.log(JSON.stringify(student, ["name", "age"]));
What stringify can't keep
Values that JSON can't represent are dropped or converted:
const item = {
name: "Pen",
price: 20,
note: undefined,
show() {},
added: new Date("2026-01-15T08:00:00Z")
};
console.log(JSON.stringify(item));
The undefined property and the method disappear, and the date becomes an ISO date string. When you parse it again, added is a plain string, not a Date; convert it back with new Date(obj.added) if you need date methods.
JSON.parse: text to object
const json = '{"product":"Maziwa","price":60,"inStock":true}';
const obj = JSON.parse(json);
console.log(obj);
console.log(obj.price + 10);
console.log(JSON.parse("[1, 2, 3]").length);
Notice that price came back as a real number, so the maths works. Before parsing, json was just a string of characters.
Invalid JSON throws
If the text breaks any JSON rule, JSON.parse throws a SyntaxError. Data from outside your program (an API, a file, storage that a user could edit) should be parsed inside try/catch:
function safeParse(text, fallback) {
try {
return JSON.parse(text);
} catch (error) {
console.log("Bad JSON:", error.name);
return fallback;
}
}
console.log(safeParse("{'name': 'Amina'}", {}));
console.log(safeParse('{"name": "Amina"}', {}));
The first string uses single quotes, so it is valid JavaScript but not valid JSON.
Parsing something that is already an object. await response.json() already returns parsed data, so calling JSON.parse on the result fails. The reverse mistake is saving an object somewhere that expects text without stringifying it first; you end up with the useless string "[object Object]".
To make an independent copy of an object, use structuredClone(obj). The old trick JSON.parse(JSON.stringify(obj)) also works for simple data, but it loses dates, undefined values and functions, as shown above.
Key points
- JSON is text: double-quoted keys and strings, and only strings, numbers, booleans,
null, arrays and objects. JSON.stringify(value, null, 2)creates (pretty) JSON; functions andundefinedare dropped and dates become strings.JSON.parse(text)rebuilds the data and throws aSyntaxErroron invalid input, so wrap outside data intry/catch.- APIs send JSON and storage holds strings, so you will use these two functions constantly.
Tip: use the ← and → keys to move between pages.
