I'm trying to load json file to an OrderedDict following mjhm's answer in Can I get JSON to load into an OrderedDict in Python?. I'm starting with just a json string for simplicity and the code is:
#!/usr/bin/python
import simplejson as json
import ordereddict
testjson='{ "foo": 1, "bar": 2 }'
my_ordered_dict = json.loads(testjson, object_pairs_hook=ordereddict.OrderedDict)
However, I get the following error when running the above:
Traceback (most recent call last):
File "./l.py", line 8, in <module>
my_ordered_dict = json.loads(testjson, object_pairs_hook=ordereddict.OrderedDict)
File "/usr/lib64/python2.6/site-packages/simplejson/__init__.py", line 318, in loads
return cls(encoding=encoding, **kw).decode(s)
TypeError: __init__() got an unexpected keyword argument 'object_pairs_hook'
I'm on python 2.6.6 and simplejson version is 2.0.9. Is there a way I can use to load json file into an OrderedDict? The aim is to read json, add some entries and then print it out preserving the order of original json file.
Edit:
After upgrading simplejson it still does not work - different version of simplejson is reported by pip and by the script. I added the following to the script:
print "simplejson version: " + json.__version__
print "simplejson path: " + json.__file__
It prints:
simplejson version: 2.0.9
simplejson path: /usr/lib64/python2.6/site-packages/simplejson/__init__.pyc
However, pip reports:
$ pip show simplejson
DEPRECATION: Python 2.6 is no longer supported by the Python core team, please upgrade your Python. A future version of pip will drop support for Python 2.6
Name: simplejson
Version: 3.10.0
Summary: Simple, fast, extensible JSON encoder/decoder for Python
Home-page: http://github.com/simplejson/simplejson
Author: Bob Ippolito
Author-email: bob@redivi.com
License: MIT License
Location: /usr/lib64/python2.6/site-packages
Requires:
Both report the same location for simplejson module and both seem to be installed:
$ ls -ld /usr/lib64/python2.6/site-packages/simplejson*
drwxr-xr-x 3 root root 4096 May 9 15:29 /usr/lib64/python2.6/site-packages/simplejson
drwxr-xr-x 2 root root 4096 May 9 15:29 /usr/lib64/python2.6/site-packages/simplejson-2.0.9-py2.6.egg-info
drwxr-xr-x 2 root root 4096 May 9 09:10 /usr/lib64/python2.6/site-packages/simplejson-3.10.0-py2.6.egg-info
How do I make sure that python uses 3.10.0 version and not the 2.0.9?