Avatar ·

How to disable replacement of Cyrillic with \\uXXXX in strings when converting to JSON

📁 Advanced Python Topics, Help, example

When trying to format JSON output using dump, Russian characters stop displaying. Something like ""arbitrary field, for example Address"": ""\\u0443\\u043b\\u0438\\u0446\\u0430 \\u041c\\u0430 etc."" appears.

I tried json.dumps().encode(""utf8"") and codac convert - nothing helps.

def load_data(filepath):n    with open(filepath, 'r', encoding='utf-8') as fh:  n        data = json.load(fh)  # load data from file into dictionary datan    retu data                    
Avatar ·

Use the parameter:

From:

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.

Example:

In [41]: with open(fn, 'r', encoding='utf-8') as f:    ...:     data = json.load(f)    ...:    ...: print(json.dumps(data,    ...:                  sort_keys=False,    ...:                  indent=4,    ...:                  separators=(','                                

Log in to leave an answer

Blogs