Unprepared (in promise) SyntaxError: Unexpected token 'in fetch function

I have several JSON files that are structured as follows (call this info.json):

{ 'data': { 'title': 'Job', 'company': 'Company', 'past': [ 'fulltime': [ 'Former Company' ], 'intern': [ 'Women & IT', 'Priority 5' ] ], 'hobbies': [ 'playing guitar', 'singing karaoke', 'playing Minecraft', ] } } 

And in a separate JavaScript file, I have a function that looks like this:

 function getJSONInfo() { fetch('info.json').then(function(response) { return response.json(); }).then(function(j) { console.log(j); }); } 

And I keep getting this error when I run getJSONInfo() :

 Uncaught (in promise) SyntaxError: Unexpected token ' 

What am I missing? I don't have anyone, ' so I'm not sure what happened.

+5
source share
2 answers

You need to have double quotes for your attributes for valid json.

You can use json validators like http://jsonlint.com/ to check the syntax is correct.

Also, as shayanypn pointed out, the "past" should be an object, not an array. You are trying to define the "past" as an object literal, but using square brackets to indicate an array.

+5
source

you are not valid at all

1- you should use double quotes

2- bad object attribute syntax

 "past": [ "fulltime": [ "Former Company" ], "intern": [ "Women & IT", "Priority 5" ] ], 

he must sleep

 "past": { "fulltime": [ "Former Company" ], "intern": [ "Women & IT", "Priority 5" ] }, 

your valid json

 { "data": { "title": "Job", "company": "Company", "past": { "fulltime": [ "Former Company" ], "intern": [ "Women & IT", "Priority 5" ] }, "hobbies": [ "playing guitar", "singing karaoke", "playing Minecraft" ] } } 
+1
source

Source: https://habr.com/ru/post/1244601/


All Articles