7

var map = new Map();
map.set('key1','value1');
map.set('key2','value2');

console.log(map);
console.log(map.toString());
console.log(JSON.parse(map.toString()))
//Uncaught SyntaxError: Unexpected token o in JSON at position 1

Converted map object to string using toString() and now I am unable to convert to map object from string.

  • “Uncaught SyntaxError: Unexpected token o in JSON at position 1” means that whatever you’re trying to parse is usually `[object Object]`, though in this case `[object Map]`. – Sebastian Simon Aug 30 '17 at 10:14
  • 2
    Is this `[object Map]` a valid JSON? – Rajesh Aug 30 '17 at 10:14
  • Hint: If it says "`[object Map]`", there isn't any more magical information hidden there. "`[object Map]`" is literally the string you're trying to parse here, from which you'll obviously never get your `value1` and `value2` back. – deceze Aug 30 '17 at 10:16
  • @Rajesh how to do that to string again? I only tried with JSON.parse – VAMSEE MOHAN KRISHNA Aug 30 '17 at 10:17
  • 1
    Map's are not JSON compatible, this is because the key can be anything, including objects etc. If your using simple key value / pairs, it might be easier to use `Object.create(null)` instead. – Keith Aug 30 '17 at 10:19
  • @deceze I want to make a string from map object and convert that string again to map? How can it be possible? – VAMSEE MOHAN KRISHNA Aug 30 '17 at 10:19
  • More info on stringifying/parsing maps in this SO question: https://stackoverflow.com/questions/28918232/how-do-i-persist-a-es6-map-in-localstorage-or-elsewhere – Me.Name Aug 30 '17 at 10:22

4 Answers4

12

To store a stringified result, better to use plain JSON object, however using Map you can create a array of entries and stringify that

var str = JSON.stringify(Array.from( map.entries()));

and then again you can parse the JSON string to array and construct a new Map

var map = new Map(JSON.parse(str))

var map1 = new Map();
map1.set('key1','value1');
map1.set('key2','value2');

var str = JSON.stringify(Array.from( map1.entries()));

//store the string somewhere or pass it to someone
//or however you want to carry it

//re construct the map again
var map2 = new Map(JSON.parse(str))

console.log('key1', map2.get('key1'));
console.log('key2', map2.get('key2'));

However, Array.from(map), or using will also return the same thing and can be used here, but someone cannot grantee what it's actually returns until execute it, on the other hand, getting an Iterator and then forming an array is more conventional and readable, however Array.from(map) might be a better solution. Also spread operator can be used over map [...map] or map.entries() [...map.entries()] to form the same array of entries.

Koushik Chatterjee
  • 4,106
  • 3
  • 18
  • 32
2

That's because the map.toString() prints [object Map] which is not parsable by JSON.parse

You should use JSON.stringify to achieve your objective

Corrected script

var map = new Map().set('key1','value1')
.set('key2','value2');

console.log(map);
//since the underlying type is array requires to be expanded [...map]
console.log(JSON.stringify([...map])); 

map2 = new Map(JSON.parse(JSON.stringify([...map])));

Remember: Maps are immutable objects, and the underlying type is an array, you have to reassign the map variable or chain the sets functions.

Full explanation can be found here: http://2ality.com/2015/08/es6-map-json.html

saniales
  • 451
  • 3
  • 14
2

var map = new Map();
map.set('key1','value1');
map.set('key2','value2');

console.log(map);
//Covert your map object into JSON object
var jsonObj=[];

map.forEach(function(val,key){
var tempObj={};
tempObj[key]=val;
jsonObj.push(tempObj)
})

console.log(JSON.stringify(jsonObj));
console.log(JSON.parse(JSON.stringify(jsonObj)))
//Uncaught SyntaxError: Unexpected token o in JSON at position 1
0

If You cannot do it with:

var map = new Map(JSON.parse(str))

Try do it using:

vat map = JSON.parse(str);

Eventually try:

var map = new Map(Object.entries(JSON.parse(str)));
sosnus
  • 968
  • 10
  • 28