'Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous>)
I am trying to parse JSON but it keeps returning
Uncaught SyntaxError: Unexpected token & in JSON at position 1
at JSON.parse (<anonymous>)
The code looks like this in VS Code:
var result = '{{ bans }}';
var res = JSON.parse(result)
But in the consol it looks like:
var result = '{'bans': [{'appealable': False, 'banend': 'Fri, 15 Jan 2021 00:00:00 GMT', 'bannedby': 'parker02311', 'banstart': 'Fri, 15 Jan 2021 00:18:58 GMT', 'id': 1, 'images': 'https://cdn.discordapp.com/attachments/642127559600898058/799867505727504464/Roblox_1_15_2021_11_07_09_PM_2.png,https://cdn.discordapp.com/attachments/642127559600898058/799863972915314688/Roblox_1_15_2021_10_53_30_PM_2.png', 'notes': '<p>test</p>', 'reasons': 'Chat Bypassing, Discrimination', 'userid': '17', 'username': 'test'}, {'appealable': False, 'banend': 'Wed, 20 Jan 2021 00:00:00 GMT', 'bannedby': 'parker02311', 'banstart': 'Sat, 16 Jan 2021 05:46:33 GMT', 'id': 2, 'images': 'https://image.prntscr.com/image/DUQMlOEKTuGDHSkvB4NuRQ.png,https://cdn.discordapp.com/attachments/642127559600898058/799874004457488394/unknown.png', 'notes': '', 'reasons': 'Chat Bypassing,Discrimination', 'userid': '202181894', 'username': 'Dagdoubleagaming'}]}';
var res = JSON.parse(result)
I am not sure where all the ''s came from because the backend looks like:
r = requests.get('http://localhost:5001/get/bans', timeout=5)
if r.status_code == 200:
jsonre = r.json()
return render_template('index.html', name=current_user.username, title='Dashboard', breadcrums=True, bans=jsonre)
The request returns:
{
"bans": [
{
"appealable": false,
"banend": "Fri, 15 Jan 2021 00:00:00 GMT",
"bannedby": "parker02311",
"banstart": "Fri, 15 Jan 2021 00:18:58 GMT",
"id": 1,
"images": "https://cdn.discordapp.com/attachments/642127559600898058/799867505727504464/Roblox_1_15_2021_11_07_09_PM_2.png,https://cdn.discordapp.com/attachments/642127559600898058/799863972915314688/Roblox_1_15_2021_10_53_30_PM_2.png",
"notes": "<p>test</p>",
"reasons": "Chat Bypassing, Discrimination",
"userid": "17",
"username": "test"
},
{
"appealable": false,
"banend": "Wed, 20 Jan 2021 00:00:00 GMT",
"bannedby": "parker02311",
"banstart": "Sat, 16 Jan 2021 05:46:33 GMT",
"id": 2,
"images": "https://image.prntscr.com/image/DUQMlOEKTuGDHSkvB4NuRQ.png,https://cdn.discordapp.com/attachments/642127559600898058/799874004457488394/unknown.png",
"notes": "",
"reasons": "Chat Bypassing,Discrimination",
"userid": "202181894",
"username": "Dagdoubleagaming"
}
]
}
Solution 1:[1]
var result = '{{ bans | escapejs }}'
Solution 2:[2]
Just taking a shot here, but I took a look here "https://www.w3schools.com/Js/js_json_parse.asp" and I think this might work. I don't think the string is in json format that's why you're getting the error.
var result = '{"bans"}'; var res = JSON.parse(result)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Henry Ecker |
Solution 2 | e_million |