It is still printing u
because there are elements in your object which are unicode
objects, and that is how python prints objects that contain unicode objects.
>>> x = {u'giga-10': [u'overview']}
>>> print x
{u'giga-10': [u'overview']}
It makes sense that you have unicode
objects, since you are deserializing JSON, and the appropriate data-structure that corresponds to JSON string is a Python 2 unicode
object.
Note, if you print a unicode
object, it doesn't print the u
, since the u
isn't actually a part of the unicode string:
>>> print u"hello"
hello
This really doesn't matter. You should just let it stop bothering you. But if you insist, for some crazy reason, to want to get rid of those u
s, then you have to convert any unicode objects inside an arbitrary object deserialized from JSON to str
types. That requires decoding the unicode object. As long as you aren't providing any hooks, the following should work for any result of json.load
:
>>> def stringify(obj):
... if isinstance(obj, unicode):
... return obj.encode('utf8')
... elif isinstance(obj, list):
... return [stringify(x) for x in obj]
... elif isinstance(obj, dict):
... return {stringify(k):stringify(v) for k,v in obj.iteritems()}
... else:
... return obj
...
>>> print stringify(x)
{'giga-10': ['overview']}
But there is no good reason to do this, unless you really, truely, need Python 2 str
, i.e. "byte-strings". You almost certainly do not, or at least, haven't indicated any reason why you would.