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?
Vadim Kotov
8,2848 gold badges51 silver badges63 bronze badges
asked Apr 11, 2017 at 20:22
user2406718
2634 silver badges16 bronze badges
-
Remove extra characters and split by comma.PM 77-1– PM 77-12017年04月11日 20:26:22 +00:00Commented Apr 11, 2017 at 20:26
-
agree, but instead of string manipulation, there may be a way to convert this string into an array.user2406718– user24067182017年04月11日 20:31:21 +00:00Commented Apr 11, 2017 at 20:31
1 Answer 1
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.
Sign up to request clarification or add additional context in comments.
1 Comment
user2406718
It worked. you are right, single quotes seems to be a problem.
Explore related questions
See similar questions with these tags.
lang-js