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 textJSON.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>