-1

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. https://filetransfer.io/data-package/64l6xzJS

Thanks for your interest. Thank you.

codeshot
  • 11
  • 3
  • `with open('18july.json', encoding='utf8') as f:` The default encoding on Windows is not UTF-8 but the file is encoded in UTF-8. Specify the encoding. – Mark Tolonen Jul 18 '22 at 16:33

1 Answers1

0

One solution:

import json
from pathlib import Path

filepath = Path("18july.json")
jsondata = json.loads(filepath.read_bytes())

print(jsondata)
Waket Zheng
  • 5,065
  • 2
  • 17
  • 30