views:

170

answers:

2

Really spent a lot of time searching for this. Please need some help.

I am trying to add multilingual feature to my web app framework. For this I am unable to send non ascii characters as JSON. Here is what I am doing

  1. Here is what I get from the database

    '\xe0\xa4\xa4\xe0\xa5\x87\xe0\xa4\xb8\xe0\xa5\x8d\xe0\xa4\xa4'

    which when I print gives me which is okay

    तेस्त

  2. I make the response object

    response = {'a':'\xe0\xa4\xa4\xe0\xa5\x87\xe0\xa4\xb8\xe0\xa5\x8d\xe0\xa4\xa4'}

  3. Send the repsonse

    import json

    sys.stdout.write(json.dumps(response))

  4. This is what it prints

    Returns u'{"a": "\u0924\u0947\u0938\u094d\u0924"}'

Any help, pointers will be welcome

Thanks!

Rushabh

+3  A: 

Is this your desired output (see ensure_ascii argument for json.dumps)?

sys.stdout.write(json.dumps(response, ensure_ascii=False))
{"a": "तेस्त"}
The MYYN
Nice solution. :)
aatifh
This is exactly what I needed. Thanks a lot!
Joel Verhagen
A: 

Hi all,

This is how it is supposed to work and it works fine!

I had a problem elsewhere and hence there were some compounding errors.

Thanks for the replies :-)

  • Rushabh
Indeed. `ensure_ascii` is required if you don't have a Unicode-safe link to the client side (typically because you've forgotten to set the page's encoding to UTF-8). But it's better if you can fix the link so you don't have to use it. Incidentally you should probably `.decode('utf-8')` your string to Unicode before passing it to `json.dumps` to clarify exactly what you mean.
bobince