V
V
venom992021-07-17 15:36:32
Python
venom99, 2021-07-17 15:36:32

How to teach JSON to understand Cyrillic using Python?

When I pull data from the database and convert it to JSON to send it to the frontend, I get incorrect data conversion to JSON due to the presence of Cyrillic. How can I teach JSON to understand Cyrillic so that the information in the second print looks the same as in the first one?

@app.route('/student', methods=['POST'])
def get_loginn():
    new_one = request.json
    s = json.dumps(new_one)
    x = json.loads(s, object_hook=lambda d: SimpleNamespace(**d))
    with sq.connect("diary.db") as con:
        cur = con.cursor()
        cur.execute("SELECT * FROM student")
        for result in cur:
            if result[0] == x.id_user:
                print(result)
                f = json.dumps(result)
                print(f)
                return jsonify(f)


Received response in the first print: (1, 'Shvetsov BE.', '21-E-95', 3, None)
Received response in the second print: [1, "\u0428\u0432\u0435\u0446\u043e\u0432 \ u0412\u0415.", "21-\u042d-95", 3, null]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
slikkness, 2021-07-17
@venom99

f = json.dumps(result, ensure_ascii=False)
print(f)

M
Mikhail Krostelev, 2021-07-18
@twistfire92

Why are you creating a json string before calling jsonify? Transfer your data immediately jsonify(result)
And so that there are no problems with the Cyrillic alphabet, write
app.config['JSON_AS_ASCII'] = False

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question