Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a dictionary to json using json.dumps having arabic characters

I have a dictionary containing arabic words like

data = [{'name': 'آدَم'}, {'name': 'آزَر'}]
print(json.dumps(data), file=open('data.json', 'a', encoding="utf-8"))

Output:

[{"name": "\u0622\u0632\u064e\u0631"}...]

I don't want to encode the arabic text while creating the data.json file. If I do not use json.dumps then it works fine but then it shows single quotes ' instead of double qoutes "

like image 969
coure2011 Avatar asked Dec 13 '22 16:12

coure2011


1 Answers

Pass the parameter ensure_ascii = False:

json.dumps(data, ensure_ascii = False)

Documentation here.

If ensure_ascii is true (the default), the output is guaranteed to have all incoming non-ASCII characters escaped. If ensure_ascii is false, these characters will be output as-is.

like image 55
Phylogenesis Avatar answered Dec 18 '22 10:12

Phylogenesis