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. This is lesson 1 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 3 lessons in total.
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.

Learn JavaScript with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 59
- Lessons
- 200
Frequently Asked Questions
Is the “JSON.parse / JSON.stringify basics” lesson free?
Yes — any 3 lessons of the JavaScript Academy learning path, including “JSON.parse / JSON.stringify basics”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 3 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.
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