JavaScript JSON

Parse and create JSON data.

✍️ 3 exercises + code challenge in this lesson

JSON (JavaScript Object Notation) is a text format for exchanging data, used by almost every web API.

  • JSON.parse(text): convert a JSON string into a JavaScript value.
  • JSON.stringify(value): convert a value into a JSON string.
  • JSON.stringify(value, null, 2): pretty-print with 2-space indentation.
In JSON, keys and strings must use double quotes. Functions, undefined and comments are not allowed.

Key Takeaways: JavaScript JSON

Mastering JavaScript JSON helps you write clean and reliable JavaScript code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

JavaScript JSON Code Example

JavaScript
<pre id="out"></pre>
<script>
const log = (...a) => document.getElementById("out").textContent += a.join(" ") + "\n";
const text = '{"name":"Sara","skills":["HTML","CSS"],"active":true}';
const user = JSON.parse(text);
log(user.name, "knows", user.skills.length, "skills");

user.skills.push("JavaScript");
log(JSON.stringify(user));
log(JSON.stringify(user, null, 2));
</script>
Free sign-in required · Google login available

JavaScript JSON Practice Exercises

Check what you learned in “JavaScript JSON”.

/ 3 solved
Fill in the blank

Convert a JSON string into an object.

const obj = JSON.(text);
✓ Correct! Not quite. Try again.
Multiple choice

Which method turns an object into a JSON string?

✓ Correct! Not quite. Try again.
Code challenge

Parse the JSON text and log the city.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services