2

Hi I am working on a simple program that takes data from a json file (input through an html form with flask handling the data) and uses this data to make calls to an API.

So I have some JSON like this:

[{"id": "ßLÙ", "server": "NA"}]

and I want to send the id to an api call like this example:

http://apicallnamewhatever+id=ßLÙ

however when i load the json file into my app.py with the following command

ids = json.load(open('../names.json'))

json.load seems to alter the id from 'ßLÙ' to 'ßLÙ'

im not sure why this happens during json.load, but i need to find a way to get 'ßLÙ' into the api call instead of the deformed 'ßLÙ'

2
  • 1
    Are you using Python 2 or Python 3? If Python 3, what is your locale set to when you call open()? Commented Sep 29, 2018 at 21:38
  • 2
    Your encoding might be windows-1252. "ßLÙ".encode("utf8").decode("windows-1252") --> 'ßLÙ' Commented Sep 29, 2018 at 21:46

1 Answer 1

4

It looks as if your names.json is encoded in "utf-8", but you are opening it as "windows-1252" [*] or something like that. Try

json.load(open('names.json', encoding="utf-8"))

and you probably should also URL-encode the id instead of concatenating it directly with that server address, something along these lines:

urllib2.quote(idExtractedFromJson.encode("utf-8")

[*] Thanks @jDo for pointing that out, I initially guessed the wrong codepage.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.