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)
  • Check this [link](https://stackoverflow.com/questions/25122371/json-dump-unicodedecodeerror-utf8-codec-cant-decode-byte-0xbf-in-position) – vb_rises May 01 '19 at 13:24
  • please give us a copy of your file users.json so that we can do testing. – jose_bacoy May 01 '19 at 13:32
  • Pedantically, the JSON format [demands UTF-8 encoding](https://datatracker.ietf.org/doc/html/rfc8259) (see section 8.1); the file does not conform to standards, although its contents are probably still perfectly sensible. – Karl Knechtel Jul 03 '22 at 22:31

1 Answers1

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

zarak
  • 663
  • 1
  • 6
  • 16