0

I have this object :

let startCourseData = {
    'slidesHistory': '[{ id: 1, difficulty: 4, performance: 100, guided_phrases: [], sequence: "A", plan: false, planned: 10 },{ id: 2, difficulty: 4, performance: 30, guided_phrases: [], sequence: "A", plan: false, planned: 0 },{ id: 3, difficulty: 4, performance: 60, guided_phrases: [], sequence: "A", plan: false, planned: 6 },{ id: 4, difficulty: 4, performance: 80, guided_phrases: [], sequence: "B", plan: false, planned: 5 },{ id: 5, difficulty: 4, performance: 80, guided_phrases: [], sequence: "B", plan: false, planned: 6 },{ id: 6, difficulty: 4, performance: 82, guided_phrases: [], sequence: "A", plan: false, planned: 10 },{ id: 7, difficulty: 4, performance: 90, guided_phrases: [], sequence: "C", plan: false, planned: 9 },{ id: 8, difficulty: 4, performance: 90, guided_phrases: [], sequence: "C", plan: false, planned: 8 },{ id: 9, difficulty: 4, performance: 90, guided_phrases: [], sequence: "B", plan: false, planned: 5 },];'
};

And I want to convert slidesHistory that is a string to an array so I used JSON.parse like this:

const slidesHistorys = JSON.parse(startCourseData.slidesHistory);
console.log(slidesHistorys);

But I get an error!

Uncaught SyntaxError: Unexpected token i in JSON at position 3

How can I fix this?

9
  • your keys does not have double quotes, like "id: 1" or in short it is not ECMA-404 conform Commented Jun 17, 2020 at 15:13
  • why use a string at all? Commented Jun 17, 2020 at 15:14
  • You have invalid JSON. I'm not sure why you're storing it this way but I suggest fixing the encoding. Commented Jun 17, 2020 at 15:14
  • The key for the object in the array should have double quotes like this: [{ "id": 1, "difficulty": 4, ....}] Commented Jun 17, 2020 at 15:14
  • I want to store slidesHistory into a database as a string.. isn't it a good idea...?? Commented Jun 17, 2020 at 15:16

1 Answer 1

1

you can use eval

let startCourseData = {
    'slidesHistory': '[{ id: 1, difficulty: 4, performance: 100, guided_phrases: [], sequence: "A", plan: false, planned: 10 },{ id: 2, difficulty: 4, performance: 30, guided_phrases: [], sequence: "A", plan: false, planned: 0 },{ id: 3, difficulty: 4, performance: 60, guided_phrases: [], sequence: "A", plan: false, planned: 6 },{ id: 4, difficulty: 4, performance: 80, guided_phrases: [], sequence: "B", plan: false, planned: 5 },{ id: 5, difficulty: 4, performance: 80, guided_phrases: [], sequence: "B", plan: false, planned: 6 },{ id: 6, difficulty: 4, performance: 82, guided_phrases: [], sequence: "A", plan: false, planned: 10 },{ id: 7, difficulty: 4, performance: 90, guided_phrases: [], sequence: "C", plan: false, planned: 9 },{ id: 8, difficulty: 4, performance: 90, guided_phrases: [], sequence: "C", plan: false, planned: 8 },{ id: 9, difficulty: 4, performance: 90, guided_phrases: [], sequence: "B", plan: false, planned: 5 },];'
};

const slidesHistorys = eval(startCourseData.slidesHistory);
console.log(slidesHistorys);

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.