Python: json.loads returns items prefixing with 'u' Python: json.loads returns items prefixing with 'u' json json

Python: json.loads returns items prefixing with 'u'


The u- prefix just means that you have a Unicode string. When you really use the string, it won't appear in your data. Don't be thrown by the printed output.

For example, try this:

print mail_accounts[0]["i"]

You won't see a u.


Everything is cool, man. The 'u' is a good thing, it indicates that the string is of type Unicode in python 2.x.

http://docs.python.org/2/howto/unicode.html#the-unicode-type


The d3 print below is the one you are looking for (which is the combination of dumps and loads) :)

Having:

import jsond = """{"Aa": 1, "BB": "blabla", "cc": "False"}"""d1 = json.loads(d)              # Produces a dictionary out of the given stringd2 = json.dumps(d)              # Produces a string out of a given dict or stringd3 = json.dumps(json.loads(d))  # 'dumps' gets the dict from 'loads' this timeprint "d1:  " + str(d1)print "d2:  " + d2print "d3:  " + d3

Prints:

d1:  {u'Aa': 1, u'cc': u'False', u'BB': u'blabla'}d2:  "{\"Aa\": 1, \"BB\": \"blabla\", \"cc\": \"False\"}"d3:  {"Aa": 1, "cc": "False", "BB": "blabla"}