Tutorial by Examples

The JSON.parse() method parses a string as JSON and returns a JavaScript primitive, array or object: const array = JSON.parse('[1, 2, "c", "d", {"e": false}]'); console.log(array); // logs: [1, 2, "c", "d", {e: false}]
A JavaScript value can be converted to a JSON string using the JSON.stringify function. JSON.stringify(value[, replacer[, space]]) value The value to convert to a JSON string. /* Boolean */ JSON.stringify(true) // 'true' /* Number */ JSON.stringify(12) // '12...
A replacer function can be used to filter or transform values being serialized. const userRecords = [ {name: "Joe", points: 14.9, level: 31.5}, {name: "Jane", points: 35.5, level: 74.4}, {name: "Jacob", points: 18.5, level: 41.2}, {name: "Jessie",...
A reviver function can be used to filter or transform the value being parsed. 5.1 var jsonString = '[{"name":"John","score":51},{"name":"Jack","score":17}]'; var data = JSON.parse(jsonString, function reviver(key, value) { return ke...
You can use a custom toJSON method and reviver function to transmit instances of your own class in JSON. If an object has a toJSON method, its result will be serialized instead of the object itself. 6 function Car(color, speed) { this.color = color; this.speed = speed; } Car.prototype.to...
JSON stands for "JavaScript Object Notation", but it's not JavaScript. Think of it as just a data serialization format that happens to be directly usable as a JavaScript literal. However, it is not advisable to directly run (i.e. through eval()) JSON that is fetched from an external source...
Not all objects can be converted to a JSON string. When an object has cyclic self-references, the conversion will fail. This is typically the case for hierarchical data structures where parent and child both reference each other: const world = { name: 'World', regions: [] }; world.region...

Page 1 of 1