user image

Jo Micheal
Published in : 2022-01-30

[solved] SyntaxError: Unexpected token o in JSON at position 1 jQuery

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?

Comments

Mohamed Atef Date : 2022-01-30

Best answers

51

Best answers

51

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);});
  1. as you see I added ` at the beginning & the end of the JSON array so be like regular text then you can pass it into the function
  2. There are comma “ , ” at the end of the last object into the Array of answers and this is a mistake  

 

Jo Micheal Date : 2022-01-30

Yea that's it the JSON code you attached is working so good, sorry this is a beginners mistakes :)

Leave a comment

Join us

Join our community and get the chance to solve your code issues & share your opinion with us

Sign up Now

Related posts

Display JSON in HTML using jQuery
Publish date: 2022-01-30 | Comments: 2

Tag: jQuery

jQuery Selector is not working .click
Publish date: 2022-02-21 | Comments: 1

Tag: jQuery

Accessing Radio button values using Jquery
Publish date: 2022-03-05 | Comments: 1

Tag: jQuery

How to get the style of element in jQuery? add it to another
Publish date: 2022-01-27 | Comments: 2

Tag: jQuery

[SOLVED] jQuery .click() not working with auto added buttons
Publish date: 2021-06-19 | Comments: 2

Tag: jQuery

How to use !important using jQuery css function?
Publish date: 2022-01-27 | Comments: 2

Tag: jQuery

jQuery alter the class of a button when I click on it?
Publish date: 2022-02-12 | Comments: 1

Tag: jQuery