codegurukool.com · Free coding tutorials & online compiler

Home › JavaScript Tutorial › JS JSON

JS JSON

Text format for data.

JSON.stringify turns objects into text and JSON.parse turns text back. Keys and strings need double quotes in JSON.

Commands in this lesson

JSON.stringify

Definition: Converts an object into JSON text.

Syntax:

JSON.stringify(value);

Example:

console.log(JSON.stringify({a: 1, b: [2, 3]}));

JSON.parse

Definition: Converts JSON text back into an object.

Syntax:

JSON.parse(text);

Example:

console.log(JSON.parse('{"a":1}').a);

Example code

<pre id='o'></pre>
<script>
const txt=JSON.stringify({name:'Ada',langs:['JS','CSS']},null,2);
const back=JSON.parse(txt);
o.textContent=txt+'\nname: '+back.name;
</script>

Run this code in the online editor

← JS fetch APIJS web APIs →