1

I have worked with Python in the backend and plain HTML in frontend. Once the data retrieved from JSON file, an object is created using them and appended to an array to send to the frontend.

Backend with Python

def unpack_coordinates(coordinates):
    coordinates_list = list()
    for item in coordinates[0]:
        coordinates_list.append({'lat': item[1], 'lng': item[0]})
    return coordinates_list

However, when checking the array in the frontend side I found & #39; added in the array.

What I got in the frontend

[{'lat': 43.770625433748776, 'lng': -79.3998992207101}, {'lat': 43.770651091617324, 'lng': -79.39977945240246}, {'lat': 43.77046066096583, 'lng': -79.39970177723474}, {'lat': 43.77043500133246, 'lng': -79.39982154480901}, {'lat': 43.770625433748776, 'lng': -79.3998992207101}]
               

I want to remove & #39; from the array. Is there any way to do it?

What I expected

[{'lat': 43.770625433748776, 'lng': -79.3998992207101}, {'lat': 43.770651091617324, 'lng': -79.39977945240246}, {'lat': 43.77046066096583, 'lng': -79.39970177723474}, {'lat': 43.77043500133246, 'lng': -79.39982154480901}, {'lat': 43.770625433748776, 'lng': -79.3998992207101}]

Or

[{lat: 43.770625433748776, lng: -79.3998992207101}, {lat: 43.770651091617324, lng: -79.39977945240246}, {lat: 43.77046066096583, lng: -79.39970177723474}, {lat: 43.77043500133246, lng: -79.39982154480901}, {lat: 43.770625433748776, lng: -79.3998992207101}]
user120242
  • 14,918
  • 3
  • 38
  • 52
CalgaryFlames
  • 678
  • 2
  • 10
  • 30
  • 1
    does https://stackoverflow.com/questions/2087370/decode-html-entities-in-python-string help? – evolutionxbox Jul 14 '20 at 20:13
  • 1
    Any Python backend should have support for sending proper JSON to the client; which backend are you using? (disregarding any and all context, you can easily replace these html entities with double quotes to get proper JSON, either on the backend or the frontend, using basic string replacement functionality) –  Jul 14 '20 at 20:16
  • @ChrisG I use flask. I tried to use string replacement functionality but in the code, it is object type so unable to use it. – CalgaryFlames Jul 14 '20 at 20:37
  • 1
    Flask uses [jsonify](https://flask.palletsprojects.com/en/1.1.x/api/#flask.json.jsonify) to send JSON, that will set the content-type and create the proper format (I also just saw that you're reading a JSON file, then sending it along; why not simply load the JSON file directly from the client-side JS?, also note that "it's not working" is a completely useless statement) –  Jul 14 '20 at 20:52
  • Note: your expected output is not valid JSON (property keys must have double quotes), and would throw an error on the client side when trying to parse it as JSON, unless you eval it as Javascript (which you shouldn't do) – user120242 Jul 14 '20 at 22:58
  • 1
    As @ChrisG has said, you should be using your framework's built-in JSON output handling, which should handle it under the covers for you. What you are doing now, returning your Python object's coerced string value to flask to output as an HTML insertable string (which is why it is escaping the string and encoding as HTML entities), is definitely not the correct way to do it. It looks like your response type is also wrong here. And additionally, it doesn't look like you are doing any important server-side processing, so using the JSON directly and transforming it client side makes more sense – user120242 Jul 14 '20 at 23:11
  • 1
    Does this answer your question? [Displaying JSON in the HTML using flask and local JSON file](https://stackoverflow.com/questions/62906140/displaying-json-in-the-html-using-flask-and-local-json-file) – user120242 Jul 15 '20 at 19:48
  • @user120242 Yes it works! – CalgaryFlames Jul 15 '20 at 20:25

0 Answers0