user3506067
user3506067

Reputation: 13

Unexpected output when saving/loading dictionary to/from json

Python 2.7.6 on windows 7 The code I use:

import json

settings = {
    "vo" : "direct3d",
    "ao" : "dsound",
    "volume" : "100",
    "priority" : "abovenormal"}

json.dump(settings, open('settings.json', 'w'))
settings = json.load(open('settings.json', 'r'))

print settings

In settings.json I get:

{"volume": "100", "priority": "abovenormal", "ao": "dsound", "vo": "direct3d"}

At the end console outputs:

{u'volume': u'100', u'priority': u'abovenormal', u'ao': u'dsound', u'vo': u'direct3d'}

What am I doing wrong?

Upvotes: 1

Views: 57

Answers (2)

msvalkon
msvalkon

Reputation: 12077

You're doing nothing wrong. json.load will convert everything to unicode, which is what the u stands for.

This should not affect the use of that dictionary, for example:

>>> result = {u'volume': u'100', u'priority': u'abovenormal', u'ao': u'dsound',
u'vo': u'direct3d'}
>>> result['volume']
u'100'

If it turns out to be a problem, you can always use simplejson which does not force the unicode. Or you can write a converter function and hook it to the json.load-function as the object_hook argument.

Here's the object_hook-example from the docs:

>>> import json
>>> def as_complex(dct):
...     if '__complex__' in dct:
...         return complex(dct['real'], dct['imag'])
...     return dct
...
>>> json.loads('{"__complex__": true, "real": 1, "imag": 2}',
...     object_hook=as_complex)
(1+2j)
>>> import decimal
>>> json.loads('1.1', parse_float=decimal.Decimal)
Decimal('1.1')

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1121734

You are doing nothing wrong; that's the exact output you should be expecting.

JSON deals exclusively with Unicode strings; the u'' strings are such Unicode values. If all your strings contain characters within the ASCII range, you'll not notice any difference when handling these.

In other words, using mixing regular (byte) strings with these values will work, Python will transparently encode and decode between the two types. You'll only notice problems when your text contains characters that fall outside of the ASCII range. Think accented characters, or symbols, or asian scripts, for example. In that case you should learn how to use Unicode properly, see the Python Unicode HOWTO.

Upvotes: 0

Related Questions