JSON.parse / JSON.stringify basics
Convert objects to JSON strings and back; use replacer/reviver briefly; handle bad input safely.
JSON.parse / JSON.stringify basics is a free JavaScript Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why JSON?
Goal: Serialize and parse safely.
- JSON.stringify → string
- JSON.parse ← string
- replacer/reviver (taste)
- Basic safety tips

stringify()
Use JSON.stringify(value) to get a JSON string. For readability, pass a space number (e.g., 2).
// Turn a value into a JSON string
const user = { name: "Ayla", score: 12, active: true };
const json = JSON.stringify(user);
console.log("json:", json);
// Optional: pretty-print with spaces (2)
const pretty = JSON.stringify(user, null, 2);
console.log("pretty:", pretty);

parse()
JSON.parse(text) returns the value. Input must be a valid JSON string.
// Parse a JSON string back to a value
const raw = "{\"name\":\"Ayla\",\"score\":12,\"active\":true}";
const value = JSON.parse(raw);
console.log("name:", value.name, "score:", value.score);

Replacer (stringify)
A replacer(key, value) can exclude or modify fields when stringifying (e.g., hide passwords).
// Replacer: filter or transform keys during stringify
const account = { id: 7, password: "secret", role: "user" };
// Hide sensitive fields
function replacer(key, value) {
if (key === "password") return undefined; // drop it
return value;
}
const safeJson = JSON.stringify(account, replacer, 2);
console.log("safeJson:", safeJson);

Reviver (parse)
A reviver(key, value) can transform values while parsing (e.g., strings → Date).
// Reviver: convert specific strings back into richer types
const orderJson = "{\"when\":\"2024-06-01T10:00:00Z\",\"total\":25}";
function reviver(key, value) {
if (key === "when" && typeof value === "string") {
return new Date(value); // restore Date
}
return value;
}
const order = JSON.parse(orderJson, reviver);
console.log("is Date?", order.when instanceof Date);
console.log("total:", order.total);

Safety & limits
Tips:
- Wrap JSON.parse in try/catch for untrusted input.
- Use replacer to drop sensitive fields before logging/sharing.
- JSON loses functions and prototype info; it serializes data only.

stringify vs parse quiz
Quick check: Serialize a value.

Recap
Recap: Convert values with JSON.stringify/JSON.parse; use a replacer to filter fields and a reviver to restore types like Date; add try/catch for safety.

Frequently asked questions
Is the “JSON.parse / JSON.stringify basics” lesson free?
Yes — the full text of “JSON.parse / JSON.stringify basics” is free to read here on the web, and the JavaScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “JSON.parse / JSON.stringify basics”?
Convert objects to JSON strings and back; use replacer/reviver briefly; handle bad input safely. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start JavaScript Academy?
No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “JSON.parse / JSON.stringify basics” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this JavaScript Academy lesson?
Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- JSON.parse / JSON.stringify basics
- Structured cloning & URLSearchParams
- Safe JSON parsing strategies