21

How can i print a json with special characters as "à" or "ç"?

I can print like this:

import json

weird_dict ={"person": "ç", "á": 'à', "ç": 'ã'}
print json.dumps(weird_dict, indent=4, sort_keys=True)

output:

{
    "person": "\u00e7", 
    "\u00e1": "\u00e0", 
    "\u00e7": "\u00e3"
}

if i use 'ensure_ascii=False'

weird_dict={"person": "ç", "á": 'à', "ç": 'ã'}
print json.dumps(weird_dict, indent=4, sort_keys=True, ensure_ascii=False)
output:
{
    "person": "ç", 
    "á": "à", 
    "ç": "ã"
}

How to overcome special characters issue using json? I need to render when i use pystache and try to print pystache.render('Hi {{person}}!', weird_dict) it occurs me:

"'ascii' codec can't decode byte 0xc3 in position 4770: ordinal not in range(128)"
ePascoal
  • 2,362
  • 6
  • 26
  • 44
  • Which version of Python? Are those Unicode strings? – Joe Sep 17 '14 at 15:01
  • `weird_json` isn't JSON, it's a Python dict. – Joe Sep 17 '14 at 15:01
  • @Joe: the output *is* JSON. – Martijn Pieters Sep 17 '14 at 15:05
  • @MartijnPieters Sorry. I claim mid-afternoon blindness. – Joe Sep 17 '14 at 15:06
  • @Joe: sure; I think the OP just tried to make a point about the output. – Martijn Pieters Sep 17 '14 at 15:06
  • What happen if you use unicode literal? `weird_dict = {u"person": u"ç", u"á": u'à', u"ç": u'ã'}` – falsetru Sep 17 '14 at 15:12
  • I dont agree that this question were already answered. i try to run the solution of @MartijnPieters marked question and it doesn't work.json_string = json.dumps(u"ברי צקלה", ensure_ascii=False).encode('utf8') print json.loads(json_string) Output: "ברי צקלה" – ePascoal Sep 17 '14 at 15:14
  • @ePascoal: The answer there assumes Python 2; drop the `encode` call. If you are using Python 2 there, you are using the wrong console encoding so your unicode literal bytes are misinterpreted. This is not a fault with Python but with your terminal encoding. – Martijn Pieters Sep 17 '14 at 15:15
  • @ePascoal: In my Mac terminal, which is correctly configured, the code you gave works just fine, for example. – Martijn Pieters Sep 17 '14 at 15:18
  • @MartijnPieters: yes, i'm using Python 2.7 in windows. How can i overcome that console encoding? Do you have any suggestion how to modify my terminal encoding? – ePascoal Sep 17 '14 at 15:19
  • @ePascoal: see [Python, Unicode, and the Windows console](http://stackoverflow.com/q/5419) – Martijn Pieters Sep 17 '14 at 15:20

1 Answers1

36

Specify ensure_ascii=False argument:

>>> import json
>>>
>>> d = {"person": "ç", "á": 'à', "ç": 'ã'}
>>> print json.dumps(d, indent=4, sort_keys=True, ensure_ascii=False)
{
    "person": "ç",
    "á": "à",
    "ç": "ã"
}

According to json module documentation:

If ensure_ascii is True (the default), all non-ASCII characters in the output are escaped with \uXXXX sequences, and the result is a str instance consisting of ASCII characters only. If ensure_ascii is False, some chunks written to fp may be unicode instances. This usually happens because the input contains unicode strings or the encoding parameter is used. Unless fp.write() explicitly understands unicode (as in codecs.getwriter()) this is likely to cause an error.

Joe
  • 46,419
  • 33
  • 155
  • 245
falsetru
  • 357,413
  • 63
  • 732
  • 636