So I have a string that looks like this:
'{utopia:1,word:2,sentence:3,tourism:4,home:5}'
I'd like to convert it to an object that looks like:
{utopia:1,word:2,sentence:3,tourism:4,home:5}
I tried using JSON.parse() on the original string but got 'SyntaxError: Unexpected token u in JSON at position 1'. I think this is because the keys of the object don't have quotations around them (in a classic JSON object, it would be like "utopia":1,...)
Not sure how to fix this. Any suggestions are much appreciated.
I think you can first replace { and } in string with empty strin then yourstring.split(',') After that [items].reduce((a, v) => ({ ...a, [v]: v}), {}) to build your object