Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encode Cyrillic characters in JSON

I want to read a JSON file containing Cyrillic symbols.

The Cyrillic symbols are represented like \u123.

Python converts them to '\\u123' instead of the Cyrillic symbol.

For example, the string "\u0420\u0435\u0433\u0438\u043e\u043d" should become "Регион", but becomes "\\u0420\\u0435\\u0433\\u0438\\u043e\\u043d".

encode() just makes string look like u"..." or adds a new \.

How do I convert "\u0420\u0435\u0433\u0438\u043e\u043d" to "Регион"?

like image 213
Влад Кныш Avatar asked Nov 03 '25 12:11

Влад Кныш


1 Answers

If you want json to output a string that has non-ASCII characters in it then you need to pass ensure_ascii=False and then encode manually afterward.

like image 70
Ignacio Vazquez-Abrams Avatar answered Nov 06 '25 02:11

Ignacio Vazquez-Abrams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!