Search code examples
pythonpython-2.7unicodeutf-8urllib2

Reading JSON from urllib2.open and UTF8 characters


I'm reading a JSON with:

# coding: utf8

import urllib2
import json

response = urllib2.urlopen('https://example.com/test.json')
data = json.load(response)

print data['mykey']

# {u'readme': u'Caf\xe9'}

but I see two things:

  • Every string is prefixed with u, i.e. type 'unicode'

  • \xe9 instead of é

How to do this properly with Python 2.7?


Solution

  • >>> import json
    >>> d = {u'readme': u'Caf\xe9'}
    >>> json.dumps(d)
    '{"readme": "Caf\\u00e9"}'
    >>> json.dumps(d, ensure_ascii=False)
    '{"readme": "Café"}'