0

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
2
  • Remove extra characters and split by comma. Commented Apr 11, 2017 at 20:26
  • agree, but instead of string manipulation, there may be a way to convert this string into an array. Commented Apr 11, 2017 at 20:31

1 Answer 1

2

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.

answered Apr 11, 2017 at 20:30
Sign up to request clarification or add additional context in comments.

1 Comment

It worked. you are right, single quotes seems to be a problem.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.