How to convert Map to JSON object in JavaScript?

To convert Map to JSON object in JavaScript, we use the Object.fromEntries method.

For instance, we write

const map1 = new Map([
  ["foo", "bar"],
  ["baz", 42],
]);

const obj = Object.fromEntries(map1);

to create a map with the Map constructor.

Then we call Object.fromEntries with map1 to convert it to an array of key-value pair arrays.