I am converting the following JSON variable to string. When back to JSON, I'm getting an error (in ReactJS, although it shouldn't be important).
var questionGlobal = {
"questionCons": [{
"string": "In",
"alignment": 2
}, {
"string": "New York State",
"alignment": 1
}, {
"string": "the",
"alignment": -1
}, {
"string": "shortest",
"alignment": -1
}, {
"string": "period",
"alignment": 0
}, {
"string": "of",
"alignment": 2
}, {
"string": "daylight",
"alignment": 0
}, {
"string": "occurs",
"alignment": 2
}, {
"string": "during",
"alignment": 1
}, {
"string": "which",
"alignment": 0
}, {
"string": "month",
"alignment": 0
}],
"options": [{
"string": "January",
"alignment": 1
}, {
"string": "December",
"alignment": 2
}, {
"string": "June",
"alignment": 1
}, {
"string": "July",
"alignment": 1
}]
};
Here is the command:
window.console.log( eval(JSON.stringify(questionGlobal)));
The output that I get in console is:
Uncaught SyntaxError: Unexpected token
Any idea where I am doing it wrong?
You could use JSON.parse
.
console.log(JSON.parse(JSON.stringify(questionGlobal)));
When using eval()
it is required that you pass a valid JS statement. One you could actually write without the eval
and without getting any errors.
JSFiddle Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With