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


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
Asked By: janeh



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.

Answered By: Ned Batchelder

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

Answered By: Aman

The u prefix means that those strings are unicode rather than 8-bit strings. The best way to not show the u prefix is to switch to Python 3, where strings are unicode by default. If that’s not an option, the str constructor will convert from unicode to 8-bit, so simply loop recursively over the result and convert unicode to str. However, it is probably best just to leave the strings as unicode.

Answered By: Abe Karplus

Unicode is an appropriate type here. The JSONDecoder documentation describe the conversion table and state that JSON string objects are decoded into Unicode objects.

From 18.2.2. Encoders and Decoders:

JSON                    Python
object                  dict
array                   list
string                  unicode
number (int)            int, long
number (real)           float
true                    True
false                   False
null                    None

"encoding determines the encoding used to interpret any str objects decoded by this instance (UTF-8 by default)."

Answered By: jdi

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


import json

d = """{"Aa": 1, "BB": "blabla", "cc": "False"}"""

d1 = json.loads(d)              # Produces a dictionary out of the given string
d2 = json.dumps(d)              # Produces a string out of a given dict or string
d3 = json.dumps(json.loads(d))  # 'dumps' gets the dict from 'loads' this time

print "d1:  " + str(d1)
print "d2:  " + d2
print "d3:  " + d3


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"}
Answered By: Mercury

Those ‘u’ characters being appended to an object signifies that the object is encoded in Unicode.

If you want to remove those ‘u’ characters from your object, you can do this:

import json, ast
jdata = ast.literal_eval(json.dumps(jdata)) # Removing uni-code chars

Let’s checkout from python shell

>>> import json, ast
>>> jdata = [{u'i': u'', u'p': u'aaaa'}, {u'i': u'', u'p': u'bbbb'}]
>>> jdata = ast.literal_eval(json.dumps(jdata))
>>> jdata
[{'i': '', 'p': 'aaaa'}, {'i': '', 'p': 'bbbb'}]
Answered By: Nivesh Krishna

I kept running into this problem when trying to capture JSON data in the log with the Python logging library, for debugging and troubleshooting purposes. Getting the u character is a real nuisance when you want to copy the text and paste it into your code somewhere.

As everyone will tell you, this is because it is a Unicode representation, and it could come from the fact that you’ve used json.loads() to load in the data from a string in the first place.

If you want the JSON representation in the log, without the u prefix, the trick is to use json.dumps() before logging it out. For example:

import json
import logging

# Prepare the data
json_data = json.loads('{"key": "value"}')

# Log normally and get the Unicode indicator
logging.warning('data: {}'.format(json_data))
>>> WARNING:root:data: {u'key': u'value'}

# Dump to a string before logging and get clean output!
logging.warning('data: {}'.format(json.dumps(json_data)))
>>> WARNING:root:data: {'key': 'value'}
Answered By: jonatan

Just replace the u’ with a single quote…

print (str.replace(mail_accounts,"u'","'"))
Answered By: Mikematic

Try this:

Answered By: 2nd Sight Lab
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.