How To Convert A Map To JSON Using JavaScript

How to Convert a Map to JSON using JavaScript

We will discover how to convert a Map to JSON using JavaScript by using two solutions. Each solution is very helpful, and you can use whatever you want.

How to convert a Map to JSON using JavaScript

Using Object.fromEntries()

You can use the fromEntries() method on a map to convert it into a JSON (Javascript object notation).

Syntax:

Object.fromEntries(map)

Parameter:

  • map: the map variable in which you want to convert.

Example:

var map = new Map()
map.set('name', 'LEARN SHARE IT');
map.set('host', 'learnshareit.com');

// Convert the map to json
var json = Object.fromEntries(map);

console.log(json);

Output: 

{name: 'LEARN SHARE IT', host: 'learnshareit.com'}

The above example initializes a new map with two map elements, the first one has key ‘name’ and value ‘LEARN SHARE IT’ while the second one has the key ‘host’ and value ‘learnshareit.com’. We then uses the  fromEntries() function to convert the map directly into the object (which is also called JSON) and the console.log() command to print out the JSON string (which actually calls the toString() on the object).

Using for each to create an object

This approach may be simple to understand, but it has the same logic, just like the first one.

Syntax:

forEach((value, key) => { /* write statements here */ } )

First, we need to look at an example to know exactly what the for each loop will return:

var map = new Map();
map.set('name', 'LEARN SHARE IT');
map.set('host', 'learnshareit.com');

// Create new json
var json = Object.create(null);

// Convert the map to json
map.forEach((value,key)=>{json[key] = value});

console.log(json);

Output: 

{name: 'LEARN SHARE IT', host: 'learnshareit.com'}

As you can see, the JSON is created by using Object.create(null) which will return a new empty object. Then the forEach() statement executes the code inside the {} brackets which, in this case, will define a new property for the object. However, if you don’t want to convert a map to json (live object) but you want to convert it to a JSON string instead. Then you can have use the JSON.stringify() function:

var map = new Map()
map.set('name', 'LEARN SHARE IT');
map.set('host', 'learnshareit.com');

// Create new json
var json = Object.create(null);

// Convert the map to json
map.forEach((value,key)=>{json[key] = value;});

console.log(JSON.stringify(json));

Output: 

{"name":"LEARN SHARE IT","host":"learnshareit.com"}

There is a little difference between JSON and a JSON string (you can realize it by paying a little attention to the quotes). In this tutorial, we will focus on converting a map to the object (JSON) only. If you want to convert a JSON back to a map then you can read How To Convert An Object To A Map Using JavaScript.

Summary

We have shown you how to convert a Map to JSON using JavaScript using some different methods. If you have questions, please provide a comment in the section below. We will reply to our visitors as soon as possible. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *