Json output with well-formed accents

I have a problem with anonymity, which is currently hard for me ... I would like to develop several web services for my own use, and currently I am struggling with my damned French accents that will correctly display on my json outputs.

Here is my scenario: I am extracting a few rows from my database which I am inserting into the dict. What I want to do next is pass this dict to json.dumps and print the result.

The problem is this: lines containing accents are displayed as utf8, so for example, this gives me the following output: it should be émilie. It is depressing that if I print each line returned, the accents will be correctly displayed in my browser.

Questions:

  • Is it okay to output such json output?
  • How can I "just" convert a dict containing accents to json? (this is vital to me as other websites will work with my output).

Here is the test I am running.

# -*- coding: utf-8 -*
from json import dumps as json_dumps
import json

machaine = "une personne émérite"
print(machaine)

output = {}
output[1] = machaine
output[2] = machaine
output[3] = machaine

jsonoutput = json_dumps(output)

print jsonoutput
+3
source share
1 answer

As the docs say, skip ensure_ascii=Falseand code manually.

+5
source

Source: https://habr.com/ru/post/1743793/


All Articles