JavaScript Tutorial
JavaScript JSON
JSON is text. JSON.stringify turns an object into a string. JSON.parse turns the string back.
Text that looks like data
JSON stands for JavaScript Object Notation. It is a string: quotes, braces, brackets, and commas. Servers send it. localStorage stores it. Your script does not send a live object over the network — it sends text.
Two functions sit on the JSON object. stringify goes object to string. parse goes string to object. That is the whole round trip.
JSON.stringify
Pass a value. You get a string. Objects and arrays become JSON text. Numbers, booleans, andnull become their JSON forms. Strings get quotes and escaped characters.
Example
const task = {
id: 1,
title: "Read chapter",
done: false,
};
const text = JSON.stringify(task);
console.log(text);
console.log(typeof text);
The log looks like an object, but typeof text is "string". You cannot write text.title. Parse first, or keep the original object.
Click Try it in JavaScript under an example. That opens/javascript/try — a live page and a console.
JSON.parse
Pass a JSON string. You get a JavaScript value. If the text is not valid JSON, parse throws — wrap it in try / catch when the string came from a user, storage, or a network.
Example
const text = '{"id":1,"title":"Read chapter","done":false}';
const task = JSON.parse(text);
console.log(task.title);
console.log(task.done);
console.log(typeof task);
JSON is stricter than JavaScript
Object keys must be in double quotes. Strings must use double quotes, not single quotes. Trailing commas are illegal. undefined, functions, and Date objects are not JSON values — stringify drops undefined fields and turns dates into strings.
Example
try {
JSON.parse("{title: 'Ada'}");
} catch (err) {
console.log(err.message);
}
try {
JSON.parse('{"title": "Ada",}');
} catch (err) {
console.log(err.message);
}
console.log(JSON.parse('{"title":"Ada"}').title);
The first two strings would be fine as JavaScript object literals. They are not JSON. The third string parses.
Pretty print
A second argument to stringify is a replacer (use null to skip it). A third argument is spaces for indent. That is for logs and files people will read, not for compact storage.
Example
const user = { name: "Lin", role: "editor" };
console.log(JSON.stringify(user, null, 2));
Where you will use it
| Place | Direction |
|---|---|
localStorage | stringify to save, parse to load |
fetch + response.json() | parse the body for you |
| Copying data | stringify then parse makes a deep clone of JSON-safe values |
response.json() on a fetch is parse under another name. Storage only keeps strings, so you stringify objects before setItem. Both chapters come later in this track.
What to remember
- JSON is text, not a live object.
JSON.stringifyobject to string.JSON.parsestring to object.- Keys and strings use double quotes. No trailing commas.
- Parse can throw. Catch it when the text is not under your control.
Next: debugging — reading the console, the error line, and fixing one problem at a time.