0

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 = '{&#39;bans&#39;: [{&#39;appealable&#39;: False, &#39;banend&#39;: &#39;Fri, 15 Jan 2021 00:00:00 GMT&#39;, &#39;bannedby&#39;: &#39;parker02311&#39;, &#39;banstart&#39;: &#39;Fri, 15 Jan 2021 00:18:58 GMT&#39;, &#39;id&#39;: 1, &#39;images&#39;: &#39;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&#39;, &#39;notes&#39;: &#39;&lt;p&gt;test&lt;/p&gt;&#39;, &#39;reasons&#39;: &#39;Chat Bypassing, Discrimination&#39;, &#39;userid&#39;: &#39;17&#39;, &#39;username&#39;: &#39;test&#39;}, {&#39;appealable&#39;: False, &#39;banend&#39;: &#39;Wed, 20 Jan 2021 00:00:00 GMT&#39;, &#39;bannedby&#39;: &#39;parker02311&#39;, &#39;banstart&#39;: &#39;Sat, 16 Jan 2021 05:46:33 GMT&#39;, &#39;id&#39;: 2, &#39;images&#39;: &#39;https://image.prntscr.com/image/DUQMlOEKTuGDHSkvB4NuRQ.png,https://cdn.discordapp.com/attachments/642127559600898058/799874004457488394/unknown.png&#39;, &#39;notes&#39;: &#39;&#39;, &#39;reasons&#39;: &#39;Chat Bypassing,Discrimination&#39;, &#39;userid&#39;: &#39;202181894&#39;, &#39;username&#39;: &#39;Dagdoubleagaming&#39;}]}';
var res = JSON.parse(result)

I am not sure where all the &#39'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"
    }
  ]
}
parker02311
  • 43
  • 1
  • 7

2 Answers2

0
var result = '{{ bans | escapejs }}'
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 05 '22 at 19:21
-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)