0

I am getting this error when I try to JSON.parse this string "['Answer 1', 'Answer 2']"

Uncaught SyntaxError: Unexpected token ' in JSON at position 1
 at Object.parse (<anonymous>)
 at <anonymous>:1:6

Can anybody tell me how to do it?

Derek Wang
10.2k4 gold badges20 silver badges40 bronze badges
asked Nov 9, 2020 at 18:45
1
  • 3
    That is not JSON. Hence why you get the error. Quotes need to be " not ' Commented Nov 9, 2020 at 18:47

1 Answer 1

2

You need to replace all single quote(') into double quote(") to parse to javascript array.

const input = "['Answer 1', 'Answer 2']";
const output = JSON.parse(input.replaceAll("'", '"'));
console.log(output);

answered Nov 9, 2020 at 18:46
Sign up to request clarification or add additional context in comments.

Comments

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.