Saving UTF-8 texts with json.dumps as UTF-8. not as a \u escape sequence In Python

This is how Saving UTF-8 texts with json.dumps as UTF-8. not as a \u escape sequence works In Python

This article will contain multiple solutions, feel free to choose the one that suits you best.

Describing : Saving UTF-8 texts with json.dumps as UTF-8. not as a \u escape sequence In Python

We want to serialize objects into Utf-8 Json strings instead of the \uXXXX format since it can not be read by humans. We want the user to be able to verify or edit the text files using json dumps.

Without the conversion to utf-8 json strings the code bellow gives us an unreadable result.

                                                                       #
import json
json_string = json.dumps("opra")
print(json_string)
                                                                       #
                                                                       #
"\u05e7\u05d1\u05\u05dc\u05d4d9 \u05e6\u05e8\u05e8"
                                                                       #

There multiple ways to achieve this goal bellow are multiple solutions that you may like. Depending on your case of course.

Solution 1 : correct way to do the conversion in Python

This is how you achieve the conversion in Python

                                                                       #
from json import dumps
result = {"symbol": "ƒ"}
json_string = dumps(result, sort_keys=True, indent=2, ensure_ascii=False)
print(json_string)
                                                                       #

And this is the result of the code

                                                                       #
{"symbol": "ƒ"}
                                                                       #

Solution 2 : use json.dump()

The second solution is to use json.dump()

                                                                       #
def jsonWrite(p, pyobj, ensure_ascii=False, encoding=SYSTEM_ENCODING, **kwargs):
with codecs.open(p, 'wb', 'utf_8') as fileobj:
json.dump(pyobj, fileobj, ensure_ascii=ensure_ascii,encoding=encoding, **kwargs)
                                                                       #

Also make sure the SYSTEM_ENCODING is configured as follows :

                                                                       #
locale.setlocale(locale.LC_ALL, '')
SYSTEM_ENCODING = locale.getlocale()[1]
                                                                       #

I hope this solution works for you, if the solutions above helped you consider donating to our Kofi account using the red button on the top of this page.

Solution 3 : use unicode-escape in your code

Another solution is to use unicode-escape in your code as follows

                                                                       #
>>import json
>>>json_string = json.dumps("ברי צקלה")
>>>json_string.encode('ascii').decode('unicode-escape')
'"ברי צקלה"'
                                                                       #

This is the only solution to give the same result which is encoded in a similar way to a file written with utf-8.

Solution 4 : json.dumps() using ensure_ascii=False before encoding value to utf-8

The last solution is to json.dumps() using ensure_ascii=False, then you should encode the value to utf-8

                                                                       #
json_string = json.dumps("ברי צקלה", ensure_ascii=False).encode('utf8')
 json_string
                                                                       #

The result is

                                                                       #
b'"\xd7\x91\xd7\xa8\xd7\x99 \xd7\xa6\xd7\xa7\xd7\x9c\xd7\x94"'
                                                                       #
                                                                       #
print(json_string.decode())
                                                                       #

The final result is the following

                                                                       #
"ברי צקלה"
                                                                       #

Summing-up

I hope this article has helped you achieve your objective, If you like the effort we did here, please consider donating to our Kofi account located at the top of this page. Keep coding, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/