63

I would like to use Python's JSON module. It was only introduced in Python 2.6 and I'm stuck with 2.5 for now. Is the particular JSON module provided with Python 2.6 available as a separate module that can be used with 2.5?

1

6 Answers 6

63

You can use simplejson.

As shown by the answer form pkoch you can use the following import statement to get a json library depending on the installed python version:

try:
    import json
except ImportError:
    import simplejson as json 
4
  • 1
    Thanks! There were so many json modules I wasn't sure which was the one used in Python 2.6.
    – moinudin
    Apr 26, 2009 at 20:40
  • 1
    Way late here, but how can you write a script to import either json or simplejson depending on the installed python version?
    – Wells
    Dec 9, 2009 at 16:46
  • 3
    @Wells: try: import json; except ImportError: import simplejson -- Excuse the indentation errors.
    – kojiro
    Oct 12, 2011 at 17:31
  • I can't believe this answer got 60 upvotes before rene edited it into something decent
    – Tim
    Sep 21, 2015 at 11:53
49

To Wells and others:

Way late here, but how can you write a script to import either json or simplejson depending on the installed python version?

Here's how:

try:
    import json
except ImportError:
    import simplejson as json 

3
  • Well, I would do it the other way round because simplejson has a faster implementation. If you have simplejson at Python >= 2.6 you should preferably use it instead of json.
    – schlamar
    Mar 12, 2012 at 13:38
  • 1
    Each one uses the performance tinfoil hat one choses. I prefer the stdlib simply because it's the stdlib. Feel free to disagree and provide benchmarks so others can agree with you.
    – pkoch
    Mar 13, 2012 at 21:46
  • 3
    simplejson has a C extension, no need for a benchmark here ;-) If the C extension is not compiled (missing compiler on installation for example) it is exactly the same library. See pypi.python.org/pypi/simplejson.
    – schlamar
    Mar 14, 2012 at 7:54
4

I wrote the cjson 1.0.6 patch and my advice is don't use cjson -- there are other problems with cjson in how it handles unicode etc. I don't think the speed of cjson is worth dealing with the bugs -- encoding/decoding json is usually a very small bit of the time needed to process a typical web request...

json in python 2.6+ is basically simplejson brought into the standard library I believe...

1

I prefer cjson since it's much faster: http://www.vazor.com/cjson.html

1
  • 1
    I want to remain compatible with the default library provided with 2.6 though, otherwise I'd agree with you.
    – moinudin
    Apr 27, 2009 at 5:58
-1

I am programming in Python 2.5 as well and wanted a suitable library. Here is how I did it.

donwloaded the simplejson egg file called simplejson-2.0.6-py2.5-linux-i686.egg from http://pypi.python.org/simple/simplejson/

installed it using the command :

sudo python ./ez_setup.py ./simplejson-2.0.6-py2.5-linux-i686.egg

Then imported the json library into the script file by doing :

import sys
sys.path.append("/home/coolkid/Android/simplejson/simplejson-2.0.6-py2.5-linux-i686.egg")
try: import simplejson as json
except ImportError: print ("import error")
-1

json is a built-in module, you don't need to install it with pip.

1
  • 1
    The first line of the documentation says: "New in version 2.6", and the question specifically ask for a module for Python 2.5
    – tjysdsg
    Mar 14, 2020 at 0:52

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.