0

I have this example code:

let testData= [{name:'Joshua',age:22,option:"[{value:'test'}]"},{name:'Ali',age:200,option:"[{value:'test2'}]"}]

let parsedData=JSON.parse(testData[0].option);
console.log(parsedData);

Testing my code using this site: https://es6console.com/

It seems that I'm unable to parse & log the data at all.

Any ideas why this is happening?

1
  • 3
    option is invalid JSON as value needs to be quoted... '[{"value":"test"}]' Commented Jun 5, 2017 at 9:48

2 Answers 2

1

Your option string is not following the JSON specs: in JSON every key and string value needs to be enclosed in double quotes. So this would work:

let testData= [{
    name: 'Joshua',
    age: 22,
    option: '[{"value": "test"}]'
}, {
    name: 'Ali',
    age: 200,
    option: '[{"value": "test2"}]'
}];

let parsedData = JSON.parse(testData[0].option);
console.log(parsedData);

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

Comments

0

I think the issue is that JSON object's fields should be wrapped in double-quotes. E.g.

let testData = [{name:'Joshua',age:22,option:`[{"value":'test'}]`},{name:'Ali',age:200,option:`[{"value":'test2'}]`}]

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.