I have a json file which happens to have a multitude of Chinese and Japanese (and other language) characters. I\'m loading it into my python 2.7 script using io.open
The JSON module handles encoding and decoding for you, so you can simply open the input and output files in binary mode. The JSON module assumes UTF-8 encoding, but can be changed using encoding
attribute on the load()
and dump()
methods.
with open('multiIdName.json', 'rb') as json_data:
cards = json.load(json_data)
then:
with open("testJson.json", 'wb') as outfile:
json.dump(cards, outfile, ensure_ascii=False)
Thanks to @Antti Haapala, Python 2.x JSON module gives either Unicode or str depending on the contents of the object.
You will have to add a sense check to ensure the result is a Unicode before writing through io
:
with io.open("testJson.json", 'w', encoding="utf-8") as outfile:
my_json_str = json.dumps(my_obj, ensure_ascii=False)
if isinstance(my_json_str, str):
my_json_str = my_json_str.decode("utf-8")
outfile.write(my_json_str)