pythonjsondictionarypersian

Converting a dictionary to json having persian characters


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": "?????? ??? ??? ???? ???????? ??????"}]} 

Solution

  • 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