Search code examples
javascriptarraysstringstring-parsing

How to convert embedded string array into array javascript


I have an array coming in string format. How do i convert it to an array.

my_data="['A','B','C']"  (Note the quotes around [])
convert this to ['A','B','C']

So far i tried json.parse and other string to array conversations without luck. Can anyone please help on this?


Solution

  • Replace the single quotes with double quotes using regex, then JSON.parse will work fine: JSON.parse(my_data.replace(/\'/g,"\""));

    Single quotes are not valid characters for JSON data.