I try to load json file then filter some data, but I failed to load it even

Hello everyone,

I’m new in learning programming, python, and in this forum as well.
I want to load one json file in VS Code and get some data from it to use it in excel.
But it just fails to load at the beginning when I write.

I wrote this code to view json file.

import json

with open("18july.json") as f:
jsondata = json.load(f)

print(jsondata)

when I run py file, it wrote in terminal

PS C:\Users\monst\Downloads\python files> & C:/Users/monst/AppData/Local/Programs/Python/Python310/python.exe "c:/Users/monst/Downloads/python files/tidyjson.py"
Traceback (most recent call last):
  File "c:\Users\monst\Downloads\python files\tidyjson.py", line 4, in <module>
    jsondata = json.load(f)
  File "C:\Users\monst\AppData\Local\Programs\Python\Python310\lib\json\__init__.py", line 293, in load    
    return loads(fp.read(),
  File "C:\Users\monst\AppData\Local\Programs\Python\Python310\lib\encodings\cp1254.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 1502: character maps to <undefined>

You can download my attached json and py file in the link below.
Also my VS Code screenshot when I run it.

Thanks for your interest.
Thank you.

If you do a plain open without specifying an encoding, it defaults to your system’s default encoding, in this case CP-1252 (aka Windows-1252). In all likelihood, your file is actually encoded with UTF-8, so simply specify that when you open the file, like so:

with open("18july.json", encoding="UTF-8") as f:
2 Likes

By jovenes via Discussions on Python.org at 18Jul2022 18:44:

I’m new in learning programming, python, and in this forum as well.
I want to load one json file in VS Code and get some data from it to use it in excel.
But it just fails to load at the beginning when I write.

I wrote this code to view json file.

import json

with open("18july.json") as f:
jsondata = json.load(f)

print(jsondata)

when I run py file, it wrote in terminal

PS C:\Users\monst\Downloads\python files> & C:/Users/monst/AppData/Local/Programs/Python/Python310/python.exe "c:/Users/monst/Downloads/python files/tidyjson.py"
Traceback (most recent call last):
 File "c:\Users\monst\Downloads\python files\tidyjson.py", line 4, in <module>
   jsondata = json.load(f)
 File "C:\Users\monst\AppData\Local\Programs\Python\Python310\lib\json\__init__.py", line 293, in load
   return loads(fp.read(),
 File "C:\Users\monst\AppData\Local\Programs\Python\Python310\lib\encodings\cp1254.py", line 23, in decode
   return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 1502: character maps to <undefined>

It looks to me like your default locale uses cp1254 for its text
encoding. This affects how the default text in files is decoded also.

JSON is inherently encoded in UTF-8, and therefore you should try
reading the file with that encoding:

with open("18july.json", encoding='utf-8') as f:
    jsondata = json.load(f)

That should get you past the JSON load. However, there was another post
recently, possibly also from you, where printing some text gave a
similar (though output based) character set error trying to print some
text. You may encounter such an error printing some of the data you have
loaded.

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

it solved Kevin, thanks a lot for ur response.

Thank you Cameron, thanks for ur valuable support.