2

I would like to transfer a dictionary to another python program, using JSON. Before that I would like to ensure the data is exactly the same after the load/dump process. Therefore I started a program to test.

>>> import json
>>> original_dict = {0: {1: 2}}
>>> original_dict == json.loads((json.dumps(original_dict)))
False

I think I missed some load/dump parameters to make it work. Please advice.

Thanks in advance

1
  • I seems that the integer key is the key reason for failure. But is it possible to send the data without modifying the data structure?
    – Winston
    Sep 3, 2013 at 2:30

4 Answers 4

3

According RFC4627:

An object structure is represented as a pair of curly brackets
surrounding zero or more name/value pairs (or members).  A name is a
string.  A single colon comes after each name, separating the name
from the value.  A single comma separates a value from a following
name.  The names within an object SHOULD be unique.

   object = begin-object [ member *( value-separator member ) ]
   end-object

   member = string name-separator value

Which means object's key must be string. In your case, you'd better use pickle instead:

>>> import pickle
>>> original_dict = {0: {1: 2}}
>>> original_dict == pickle.loads((pickle.dumps(original_dict)))
True
2
  • Thanks. It seems there is no way to use JSON ATM. But pickle (or cPickle) is non-standard. That's a tough decision to me.
    – Winston
    Sep 3, 2013 at 2:34
  • @Winston: Of course there is a way, you just need to find out what you will need and then define a mapping between Python object and value serialized by JSON. Simple example for single level dict is this: {0: 'a'} == dict(json.loads(json.dumps({0: 'a'}.items()))) (the result is True). For more complex objects just use your imagination.
    – Tadeck
    Sep 3, 2013 at 2:42
1

You're missing a subtlety about JSON itself. All keys in objects are strings. So, when you convert your original dict to json, it looks like:

>>> json.dumps(original_dict)
'{"0": {"1": 2}}'

And then when you load it back,

>>> json.loads(json.dumps(original_dict))
{u'0': {u'1': 2}}

and you can see that the keys remain as strings (or more precisely, unicode). If you had originally used

>>> original_dict = {'0': {'1': 2}}
>>> json.loads(json.dumps(original_dict)) == original_dict
True

you would see what you expected.

That said, if you need to exactly preserve some arbitrary data, json may not be the way to go. You're probably better off with one of the suggestions in the other answers.

1

The reason it is not working is that you integer keys get converted to string:

>>> print original_dict
{0: {1: 2}}
>>> print json.dumps(original_dict)
{"0": {"1": 2}}

I recommend you use shelve:

import shelve

original_dict = {0: {1: 2}}
with shelve.open('test.db') as db: 
    db['mydict'] = original_dict   # store the dict

Now test it all works:

original_dict = {0: {1: 2}}
with shelve.open('test.db') as db: 
    retrieved_dict = d['mydict']   
print retrieved_dict == original_dict   # True
0

how about serializing object using pickle lib and calculate result string MD5 hash?

1
  • Since JSON has higher interoperability than pickle, I would prefer JSON. Thanks for your advice.
    – Winston
    Sep 3, 2013 at 2:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.