A
A
Anton2017-04-05 16:30:40
Python
Anton, 2017-04-05 16:30:40

After converting the object to json format (json.dumps), escaped ascii characters are displayed instead of text. How to fix?

Strings come from outside in different encodings.
I need to form a json object from them, but after conversion, escaped characters are displayed instead of text.
I tried to encode all strings in utf-8
and wrote such things:

json = json.dumps(j, ensure_ascii=False, encoding="utf-8")

but all in vain.
I don't quite understand what's going on.
# -*- coding: utf-8 -*-

status = get_text()
obj = {
    "status" : status
}
jsonStatus = json.dumps(obj)

before converting to json, SL-666 is output
after \\u0421\\u041b-666

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Khromov, 2017-04-05
@JetBaget

You need to add the ensure_ascii=False argument to the json.dumps call, like this:

import json

a = 'СЛ-666'
print(json.dumps(a, ensure_ascii=False))

Conclusion:
"СЛ-666"

Process finished with exit code 0

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question