Converting a dictionary to json having persian characters

2.1k Views Asked by At

Here is some code of mine, I'm trying to convert a dictionary to json having Persian characters but I get question marks instead of characters. My dictionary looks like this:

bycommunity("0": [{"60357": "این یک پیام است"}] )

with open('data.json', 'wb') as f:
f.write(json.dumps(bycommunity).encode("utf-8"))

the result is :

{"0": [{"60357": "?????? ??? ??? ???? ???????? ??????"}]} 
2

There are 2 best solutions below

3
Mohamed Mahmoud On BEST ANSWER
data = {"0": [{"60357": "این یک پیام است"}]} 
with open('data.json', 'w') as f:
  json.dump(data, f, ensure_ascii=False)

and also check this Answer for more details

0
s. Houshmand On
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf: 
        jsonf.write(json.dumps(data, ensure_ascii=False, indent=4))