-2

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?

Community
  • 1
  • 1
Lidia
  • 2,005
  • 5
  • 25
  • 32

1 Answers1

0

object_pairs_hook was added to simplejson in version 2.1.0. Your version is too old; update it.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • Tried `pip install -Iv simplejson 2.1.0` but got: No matching distribution found for 2.1.0. Not sure what version to install that will work with python 2.6.6. Do I need to worry about compatibility issues? – Lidia May 04 '17 at 23:51
  • `sudo pip install simplejson --upgrade` did the trick: Successfully installed simplejson-3.10.0 – Lidia May 05 '17 at 00:16
  • Got the same `unexpected keyword argument` error with simplejson version 3.6.5. Works only after upgrading to the latest - 3.10.0. Also, had to use --proxy= with pip install command. – Lidia May 09 '17 at 16:20
  • Actually, it's not working. I'll update my question. – Lidia May 10 '17 at 00:45