JavaScript

Convert JSON to Array/map – JavaScript

JSON, also known as “JavaScript Object Notation”, is commonly used for transmitting data between a server and a web application. JSON is a simple text-based format that can be simply processed and turned into JavaScript objects such as arrays, maps, and so on.

This tutorial will illustrate the ways to convert JSON to an array and map in JavaScript.

How to Convert JSON to JavaScript Array?

To convert JSON to an array, use the “JSON.parse()” method. This method is used for converting the JSON string to an object. For converting the JSON string to an array, we will first convert the string to an object and then call the “push()” method for adding values in an empty array.

Syntax

Use the following method for the “JSON.parse()” method:

JSON.parse(JSONString);

It accepts a JSON string as an input and gives an object as an output.

Example

Create a JSON string named “strJSON ”:

var strJSON ='{"name": "Paul", "age": "32", "Designation": "HR"}';

Invoke the “JSON.parse()” method by passing the JSON string as an argument that converts it into an object:

var objJSON = JSON.parse(strJSON);

Create an empty array named “arrayJSON”:

var arrayJSON = [];

Iterate the converted object called “objJSON” and push the values against each key to the array using the “push()” method:

for(var i in objJSON ){
 arrayJSON.push(objJSON[i]);
}

Finally, print the array on the console:

console.log(arrayJSON);

It can be seen that the JSON has been successfully converted to an array:

How to Convert JSON to map in JavaScript?

For converting JSON to map, use the “Map()” constructor with the “Object.entries()” and “JSON.parse()” methods. The “JSON.parse()” method converts the string into an object, while the “Object.entries()” method gives an array of key-value pairs from the converted object. The “Map” constructor creates a new Map object from the array of entries.

Syntax

Use the given syntax for converting JSON to map in JavaScript:

new Map(Object.entries(JSON.parse(objJSON)));

Example

Call the “Map()” constructor with “Object.entries()” and “JSON.parse()” method by passing the JSON string as an argument:

const mapJSON = new Map(Object.entries(JSON.parse(objJSON)));

Print the map on the console using “console.log()” method:

console.log(mapJSON);

Output

We have provided all the necessary information related to the conversion of JSON to array/map in JavaScript.

Conclusion

For converting JSON to an array, use the “JSON.parse()” method with the “push()” method, and to convert JSON to a map, utilize the “Map()” constructor with the “Object.entries()” and “JSON.parse()” method. JSON.parse() method is the key method for conversion as it converts the JSON string to an object. This tutorial illustrated the methods to convert JSON to an array and map in JavaScript.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.