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
Abhisar Tripathi
1,6612 gold badges13 silver badges21 bronze badges
1 Answer 1
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
Derek Wang
10.2k4 gold badges20 silver badges40 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js
"not'