Fix Python – Python: json.loads returns items prefixing with ‘u’


Asked By – janeh

I’ll be receiving a JSON encoded string from Objective-C, and I am decoding a dummy string (for now) like the code below. My output comes out with character ‘u’ prefixing each item:

[{u'i': u'', u'p': u'aaaa'}, {u'i': u'', u'p': u'bbbb'}...

How is JSON adding this Unicode character? What’s the best way to remove it?

mail_accounts = []
da = {}
    s = '[{"i":"","p":"aaaa"},{"i":"","p":"bbbb"},{"i":"","p":"ccccc"},{"i":"","p":"ddddd"},{"i":"","p":"eee"}]'
    jdata = json.loads(s)
    for d in jdata:
        for key, value in d.iteritems():
            if key not in da:
                da[key] = value
                da = {}
                da[key] = value
except Exception, err:
    sys.stderr.write('Exception Error: %s' % str(err))

print mail_accounts

Now we will see solution for issue: 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.

This question is answered By – Ned Batchelder

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0