Nearby lessons

22 of 22

JavaScript: JSON.parse() & JSON.stringify()

📌 What are JSON Methods?

JSON.stringify() converts JavaScript objects/arrays into a JSON string, while JSON.parse() converts JSON strings back into JavaScript objects.

1️⃣ Convert Object to JSON String

JSON.stringify() is used to convert a JavaScript object to a JSON string.

Example02
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

2️⃣ Convert JSON String to Object

JSON.parse() is used to convert a JSON string back into a JavaScript object.

Example03
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

3️⃣ Nested Object Conversion

Nested objects are also supported when using JSON.stringify() or JSON.parse().

Example04
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

4️⃣ Convert Array to JSON and Back

You can also use JSON methods with arrays.

Example05
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

5️⃣ Use Replacer Parameter with JSON.stringify()

You can specify which properties to include by using the replacer array in JSON.stringify().

Example06
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

6️⃣ Beautify JSON Output with Indentation

JSON.stringify() can format output using the third parameter (space).

Example07
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3