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
as follows:
with io.open('multiIdName.json', encoding="utf-8") as json_data:
cards = json.load(json_data)
I add a new property to the json, all good. Then I attempt to write it back out to another file:
with io.open("testJson.json",'w',encoding="utf-8") as outfile:
json.dump(cards, outfile, ensure_ascii=False)
That's when I get the error TypeError: must be unicode, not str
I tried writing the outfile as a binary (with io.open("testJson.json",'wb') as outfile:
), but I end up with stuff this:
{"multiverseid": 262906, "name": "\u00e6\u00b8\u00b8\u00e9\u009a\u00bc\u00e7\u008b\u00ae\u00e9\u00b9\u00ab", "language": "Chinese Simplified"}
I thought opening and writing it in the same encoding would be enough, as well as the ensure_ascii flag, but clearly not. I just want to preserve the characters that existed in the file before I run my script, without them turning into \u's.