EN
JavaScript - convert json text to dictionary (map / plain object) variable
13
points
In this article, we would like to show you how to convert a JSON text to a dictionary (map / plain object) variable in JavaScript.
In the first versions of JavaScript it was possible to create plain objects and make 5 operations on them: add, update, check, delete and iterate items (called properties). It was called the prototyping process. This way programmers used objects such as Map (Dictionary or associate array) to solve some missing features. ECMAScript 2015 (6th Edition) introduced Map type.
1. JSON.parse
example
// ONLINE-RUNNER:browser;
var json = '{"key 1":"value 1", "key 2":"value 2", "key 3":"value 3"}';
var dictionary = JSON.parse(json);
console.log(dictionary['key 1']);
console.log(dictionary['key 2']);
console.log(dictionary['key 3']);
Note: abowe example is nothing more like deserialzaion of json with plain object inside.