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?

1

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.