JSON.parse() & JSON.stringify() (JavaScript)
JSON.parse() turns a JSON string into a JavaScript object, and JSON.stringify() turns a JavaScript value into a JSON string. Together they convert data to and from text.
JavaScript
Use JSON.parse() when you receive JSON text (for example from an API or localStorage) and need a usable object.
Use JSON.stringify() when you need to send or store data as text. A common trick for a quick deep copy of plain data is JSON.parse(JSON.stringify(obj)).
Syntax
JSON.parse(text) / JSON.stringify(value)Examples
JavaScript
const obj = { id: 1, active: true };
const text = JSON.stringify(obj);
console.log(text);Output:
{"id":1,"active":true}JavaScript
const back = JSON.parse('{"id":1,"active":true}');
console.log(back.id);Output:
1