Jo Micheal
30 Jan 2022
jQuery
I am trying to use JSON.parse() to convert the text of JSON to objects but when I run the code I get this error
SyntaxError: Unexpected token o in JSON at position 1
The JSON example which I am using is
jQuery(document).ready(($) => {
//Create variable to store the JSON
var data = [
{
"id": 3,
"title": "Lets narrow down your selection",
"subtitle": "What services do you want to include in your selection?",
"answers":[
{ "name": "Driver Profiling", "value": "0" },
{ "name": "Driver Assessment", "value": "1" },
{ "name": "Driver Training", "value": "2" },
{ "name": "Driver Management", "value": "3" },
{ "name": "Driver License check", "value": "4" },
]
},
];
let test = JSON.parse(data);
console.log(test);
});
What should I do?
HII,
This must be a JSON typing error because the JS code is totally correct,
I noticed that you not using ‘ or " to store the JSON into the variable and that’s a mistake you should use it like
jQuery(document).ready(($) => {
//Create variable to store the JSON
var data = `[
{
"id": 3,
"title": "Lets narrow down your selection",
"subtitle": "What services do you want to include in your selection?",
"answers":[
{ "name": "Driver Profiling", "value": "0" },
{ "name": "Driver Assessment", "value": "1" },
{ "name": "Driver Training", "value": "2" },
{ "name": "Driver Management", "value": "3" },
{ "name": "Driver License check", "value": "4" }
]
},
]`;
let test = JSON.parse(data);
console.log(test);
});
Jo Micheal
30 Jan 2022
Yea that's it the JSON code you attached is working so good, sorry this is a beginners mistakes :)
© 2024 Copyrights reserved for web-brackets.com