agyanadda@gmail.com

JS JSON

Arrays and Objects 1 min read Updated Dec 1, 2026 Topic 24 of 38

JSON (JavaScript Object Notation) is a text format for sending and saving data. APIs and config files use it everywhere.

{ "name": "Asha", "age": 20, "skills": ["HTML", "CSS"] }

JSON rules

  • Keys must be in double quotes
  • Strings use double quotes, not single
  • No trailing commas, no comments, no functions

Convert

  • JSON.stringify(obj) → object to JSON text
  • JSON.parse(text) → JSON text to object

JSON.stringify(obj, null, 2) prints it nicely indented.

If the text is not valid JSON, JSON.parse throws an error — wrap it in try...catch when the data comes from outside.

Example

Example — stringify and parse HTML
<pre id="out"></pre>

<script>
  const course = { title: 'JavaScript', topics: 40, free: true, tags: ['web', 'frontend'] };

  const text = JSON.stringify(course, null, 2);
  const back = JSON.parse(text);

  document.getElementById('out').textContent =
    'As JSON text:\n' + text + '\n\nParsed back – title: ' + back.title + ', type: ' + typeof back;
</script>

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which method turns a JSON string into an object?

Exercise 2 True / False

JSON keys can use single quotes.

Exercise 3 Fill in the Blank

JSON.___ converts an object into a JSON string.

Frequently Asked Questions

No. Almost every language (PHP, Python, Java) can read and write JSON, which is why APIs use it.