views:

198

answers:

1

Hello,

I have an anoying problem that is giving me a hard time these days... I would like to develop a few webservices for my own usage and currently i am fighting with my damn french accents to be rendered correctly in my json outputs.

Here is my scenario: I retrieve a number of lines from my database that i put in a dict. What i want to do next is pass this dict to json.dumps and output the result.

The problem is: strings containing accents are rendered as utf8 so for example it gives me the following ouput \u00e9milie (it should be émilie). What is frustrating is that if i print each returned line, accents will be correctly rendered in my browser.

Questions:

  • Is it normal to give this kind of json output ?
  • How can I 'simply' transform a dict containing accents to json ? (whis is vital for me since 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
+1  A: 

Just like the docs say, pass ensure_ascii=False and encode manually.

Ignacio Vazquez-Abrams
just like the doc says... Sorry for my apparent lazyness. I didn't understood what was the purpose of this parameter.Thank you for your quick and clear answer.
out of curiosity do you know why this is not the default behavior ? Isn't it its first purpose to output correctly encoded characters ?
They are correctly encoded. The JSON spec allows characters to be encoded using escapes instead of the character itself. And if all you're allowed to use is ASCII then you *must* use Unicode escapes. http://json.org/
Ignacio Vazquez-Abrams