codegurukool.com · Free coding tutorials & online compiler

Home › Node.js Tutorial › Node.js JSON

Node.js JSON

Convert data to and from text.

JSON.parse turns JSON text into an object and JSON.stringify turns an object into text. Node.js uses JSON for APIs and config files.

Commands in this lesson

JSON.parse

Definition: Converts JSON text back into an object.

Syntax:

JSON.parse(text);

Example:

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

JSON.stringify

Definition: Converts an object into JSON text.

Syntax:

JSON.stringify(value);

Example:

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

Example code

const o = JSON.parse('{"name":"Asha","age":20}');
console.log(o.name);
console.log(JSON.stringify({ ok: true }));

Run this code in the online editor

← Node.js URL and query stringsNode.js process →