Looking for help parsing a string into a JSON object and converting it to a javascript array. Here is the initial string:
"[{\"items\":\"nut\",\"sales\":6,\"prices\":10},\n {\"items\":\"bolt\",\"sales\":8,\"prices\":20},\n {\"items\":\"cam\",\"sales\":0,\"prices\":15},\n {\"items\":\"cog\",\"sales\":3,\"prices\":20}]"
After removing the newline character, we get:
"{\"items\":\"nut\",\"sales\":6,\"prices\":10}, {\"items\":\"bolt\",\"sales\":8,\"prices\":20}, {\"items\":\"cam\",\"sales\":0,\"prices\":15}, {\"items\":\"cog\",\"sales\":3,\"prices\":20}"
I attempted the following code:
dd = "[{\"items\":\"nut\",\"sales\":6,\"prices\":10},\n {\"items\":\"bolt\",\"sales\":8,\"prices\":20},\n {\"items\":\"cam\",\"sales\":0,\"prices\":15},\n {\"items\":\"cog\",\"sales\":3,\"prices\":20}]";
dd = dd.replace(/\\n/g, '');
dd = dd.replace(/[\[\]']+/g,'');
console.log(JSON.parse(dd));
dd= JSON.parse(dd));
However, the JSON doesn't parse correctly as dd.items returns null.