0

I have a file called test.json with the following content.

{
"Nederbördsmängd": "7",
"Rådande väder": "13"
}

I want to load it into my python script as a json-object.

with open("test.json",encoding='UTF-8') as json_data_file:
        schema = json.load(json_data_file)
print(json.dumps(schema,indent=2))

The output:

{
  "Nederb\u00f6rdsm\u00e4ngd": "7",
  "R\u00e5dande v\u00e4der": "13"
}

When I print the file directly without converting it to a json-object it works as expected.

with open("test.json",encoding='UTF-8') as json_data_file:
        print(json_data_file.read())

The output:

{
"Nederbördsmängd": "7",
"Rådande väder": "13"
}

How do I load my file into my script as a json object without altering its content? I'm using python 3.9.7.

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
JabbaThePadd
  • 97
  • 1
  • 7
  • 1
    To clarify: your issue is _not_ in `json.loads()`, which loads the file correctly (try `print(schema)`, which gives the exact output you expect). The issue is that when you _dump_ the object back to json, the default behavior of `json.dumps()` is to have `ensure_ascii=True`, which forces those unicode characters to be encoded as ASCII. Setting `ensure_ascii=False` in `json.dumps()` fixes this issue. – Pranav Hosangadi May 06 '22 at 15:16
  • When I used to work with python it was encoding="utf-8". I'm not sure if the capital letters might be an issue –  May 06 '22 at 15:22

0 Answers0