0Pricing
JavaScript Academy · Lesson

JSON.parse / JSON.stringify basics

Convert objects to JSON strings and back; use replacer/reviver briefly; handle bad input safely.

Why JSON?

Goal: Serialize and parse safely.

  • JSON.stringify → string
  • JSON.parse ← string
  • replacer/reviver (taste)
  • Basic safety tips
JSON.parse / JSON.stringify basics — illustration 1

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);
JSON.parse / JSON.stringify basics — illustration 2

All lessons in this course

  1. JSON.parse / JSON.stringify basics
  2. Structured cloning & URLSearchParams
  3. Safe JSON parsing strategies
← Back to JavaScript Academy