EN
JavaScript - parse json
6
points
In this short article we would like to show how to convert JSON to object in JavaScript.
The article is focsed on JSON.parse()
method, but at the beginning we try to present basic definitions in simple way.
Note:Ā scroll to below exmple if you know well definitions.
Simple definitions:
Term | Description |
JSON |
Approach to preset JavaScript objectĀ as text (can be stored asĀ string type in JavaScript VM). So:
|
object | Stored in memory data strucure. |
JSON to string conversion |
Operation that deserilises / converts text to object. We can do it in few ways:
|
Quick solution:
// ONLINE-RUNNER:browser;
var json = '{"id": 2, "name": "Tom", "age": 25}';
var object = JSON.parse(json);
console.log(object); // [object Object]
console.log(object.id); // 2
console.log(object.name); // Tom
console.log(object.age); // 25
console.log(object.address); // undefined