2

I am using Python 2.6 to load a JSON string. I want to maintain the key order of the string when I load. I tried using the answer here: Can I get JSON to load into an OrderedDict in Python?

and I made sure to import simplejson as json since I am on Python 2.6, but I am still receiving this error:

TypeError: __init__() got an unexpected keyword argument 'object_pairs_hook'

What am I missing?

Here is code (truncated):

import simplejson as json
import ordereddict
theJson = json.loads('{valid json}', object_pairs_hook=ordereddict.OrderedDict)

same error if I use import json instead of simplejson

Community
  • 1
  • 1
Seephor
  • 1,692
  • 3
  • 28
  • 50

1 Answers1

1

You could provide an updated local copy of simplejson.

Download the simplejson source:

git clone https://github.com/simplejson/simplejson.git

Compile simplejson:

cd simplejson
python setup.py build

Package simplejson as an egg (zipped Python package):

python setup.py bdist_egg

The simplejson egg will be located at:

dist/simplejson-<version>-<python>-<os>-<arch>.egg

Where <version> is the simplejson version, <python> is the Python version, <os> is the operating system, and <arch> is the processor architecture. On my machine this resulted in:

dist/simplejson-3.10.0-py2.7-linux-x86_64.egg

Copy the simplejson egg to somewhere accessible (this could simply be the local directory of your application). Now in your Python application, you have to tell Python where it can find the new version of simplejson:

import sys

SIMPLEJSON_EGG = 'path/to/simplejson-....egg'
sys.path.insert(1, SIMPLEJSON_EGG)

import simplejson
Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144