encode means characters to binary. What you want when reading a file is binary to charactersdecode. But really this entire process is way too manual, simply do this:

with open('keys.json', encoding='utf-8') as fh:
    data = json.load(fh)

print(data)

with handles the correct opening and closing of the file, the encoding argument to open ensures the file is read using the correct encoding, and the load call reads directly from the file handle instead of storing a copy of the file contents in memory first.

If this still outputs invalid characters, it means your source encoding isn't UTF-8 or your console/terminal doesn't handle UTF-8.

Answer from deceze on Stack Overflow
🌐
Python
docs.python.org › 3 › library › json.html
JSON encoder and decoder — Python 3.14.3 documentation
The RFC requires that JSON be represented using either UTF-8, UTF-16, or UTF-32, with UTF-8 being the recommended default for maximum interoperability.
🌐
Python Forum
python-forum.io › thread-1338.html
Python .json problem with UTF-8 file
When my program tries to deserialize a .json file, it chokes on the UTF-8 designation (EF BB BF) at the beginning of the .json file. The error is: 'No JSON object could be decoded' Is there a way to ignore those three characters while reading ...
🌐
Reddit
reddit.com › r/learnpython › can't load json - various encoding problems
r/learnpython on Reddit: Can't load JSON - various encoding problems
June 17, 2019 -

I'm trying to import this JSON file into my python code. When I saved it as a Unicode file, I get

UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 183623: character maps to <undefined>

or this error when I tried changing the encoding type to UTF-8:

json.decoder.JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig): line 1 column 1 (char 0)

my code to open it is just

with open(filepath) as f:
statdata = json.load(f)
  • the encoding argument in open for when I tried to import in UTF8

EDIT: seems to work OK with open(filepath, encoding='utf-8-sig')

🌐
PYnative
pynative.com › home › python › json › python encode unicode and non-ascii characters as-is into json
Python Encode Unicode and non-ASCII characters as-is into JSON
May 14, 2021 - Done writing JSON serialized Unicode Data as-is into file Reading JSON serialized Unicode data from file Decoded JSON serialized Unicode data 明彦 明彦 ... You can also set JSON encoding to UTF-8. UTF-8 is the recommended default for maximum ...
🌐
Python
docs.python.org › 3.0 › library › json.html
json — JSON encoder and decoder — Python v3.0.1 documentation
Deserialize fp (a .read()-supporting file-like object containing a JSON document) to a Python object. If the contents of fp are encoded with an ASCII based encoding other than UTF-8 (e.g. latin-1), then an appropriate encoding name must be specified.
🌐
Stuff
nexusger.de › posts › 2015-11-02-python-pandas-and-json_read-with-utf-8-encoding
Python, pandas and json_read with utf-8 encoding · Stuff
The pandas library is a fantastic python toolkit to work with data. Recently I needed to read some json files in a pandas dataframe. Usually you can do that easily with the built in method: ... But this method fails, if it encounters utf-8 encoded files. In contrast to the more often used methods _read_table_ and _read_csv_, _read_json_ does not provide an _encoding_ parameter.
🌐
Readthedocs
simplejson.readthedocs.io › en › v3.17.0
simplejson — JSON encoder and decoder — simplejson 3.17.0 documentation
If fp.read() returns bytes, such as a file opened in binary mode, then an appropriate encoding should be specified (the default is UTF-8). ... load() will read the rest of the file-like object as a string and then call loads(). It does not stop at the end of the first valid JSON document it ...
Find elsewhere
🌐
Lightrun
lightrun.com › answers › ytdl-org-youtube-dl-retrieve-json-data-in-unicode-encoding-utf-8-
Retrieve JSON data in unicode (Encoding UTF-8)
GitHub Actions, Python October 8, 2023 · Solving view registration conflicts in React Native SVG animations. 18,342 2 Years ago Read More · Understanding environment variables in GitHub Actions workflows. 31,209 1 Year ago Read More · Fixing type generation errors in Swagger TypeScript API.
🌐
Medium
alucard001.medium.com › quick-notes-how-to-write-utf-8-json-content-to-file-no-u-escape-using-python-3-72df2889f54
Quick notes “How to write UTF-8 JSON content to file, no \u escape using Python 3 | by Ellery Leung | Medium
August 14, 2019 - Here is a quick note to this question: How to write a JSON Unicode (e.g. in Chinese) string to a file, without Python 3 json module to convert your string to \u escaped string. Searching on Google I found that there are a lot of Python 2 syntax which is not applicable to Python3. So I decided to write this for reference to other people. Here is the example. with open(your_filename, 'r') as f: # Read file to a variable json_content = json.loads(f.read()) # Write json_content to file.
🌐
GeeksforGeeks
geeksforgeeks.org › python-encode-unicode-and-non-ascii-characters-into-json
Python Encode Unicode and non-ASCII characters into JSON - GeeksforGeeks
April 28, 2025 - How to encode Unicode and non-ASCII characters into JSON in Python. How to save non-ASCII or Unicode data as-is, without converting it to a \u escape sequence, in JSON. How to serialize Unicode data and write it into a file. How to serialize Unicode objects into UTF-8 JSON strings, instead of \u escape sequences.
🌐
Python
docs.python.org › 2 › library › json.html
18.2. json — JSON encoder and decoder — Python 2.7.18 documentation
Deserialize fp (a .read()-supporting file-like object containing a JSON document) to a Python object using this conversion table. If the contents of fp are encoded with an ASCII based encoding other than UTF-8 (e.g. latin-1), then an appropriate encoding name must be specified.
🌐
Real Python
realpython.com › python-json
Working With JSON Data in Python – Real Python
August 20, 2025 - To investigate where Python managed to remove even more whitespace from the original JSON, open the Python REPL again and minify the content of the original hello_frieda.json file with Python’s json module: ... >>> import json >>> with open("hello_frieda.json", mode="r", encoding="utf-8") as input_file: ... original_json = input_file.read() ...