1

I try to open a json file but get a decode error. I can't find the solution for this. How can i decode this data?

The code gives the following error:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf6 in position 3765: invalid start byte

import json
url = 'users.json'
with open(url) as json_data:
     data = json.load(json_data)
3
  • Check this link Commented May 1, 2019 at 13:24
  • please give us a copy of your file users.json so that we can do testing. Commented May 1, 2019 at 13:32
  • Pedantically, the JSON format demands UTF-8 encoding (see section 8.1); the file does not conform to standards, although its contents are probably still perfectly sensible. Commented Jul 3, 2022 at 22:31

1 Answer 1

3

That means that the data you're trying to decode isn't encoded in UTF-8

EDIT:

You may decode it before loading it with json using something like this:

with open(url, 'rb') as f:
  data = f.read()
  data_str = data.decode("utf-8", errors='ignore')
  json.load(data_str)

https://www.tutorialspoint.com/python/string_decode.htm

Be careful that you WILL lose some data during this process. A safer way would be to use the same decoding mechanism used to encode your JSON file, or to put raw data bytes in something like base64

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

1 Comment

should it not be json.loads(... as in loading a string?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.