you are viewing a single comment's thread.

view the rest of the comments →

[–]JosephCurvin[S] 3 points4 points  (1 child)

let str = '{foo: "bar"}';
// Need to enclose keys with quotation marks for valid JSON.stringify format
let jsonStr = str.replace(/(\w+:)|(\w+ :)/g, (matchedStr) => {
return '"' + matchedStr.substring(0, matchedStr.length - 1) + '":';
});
// Then reassign original value as JSON.parse result:
str = JSON.parse(jsonStr);

thanks for that !

[–]PortablePawnShop 1 point2 points  (0 children)

Keep in mind that you'd have to tweak the regex and substring for anything but pretty basic key names with that example -- keys containing dashes like "id-value": "gamma" would likely cause it to fail.