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?
6 Answers
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

- 1
- 1

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

- 2,682
- 1
- 19
- 17
-
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 '12 at 13:38
-
1Each 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 '12 at 21:46
-
3simplejson 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 http://pypi.python.org/pypi/simplejson/. – schlamar Mar 14 '12 at 07:54
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...

- 678
- 1
- 4
- 7
I prefer cjson since it's much faster: http://www.vazor.com/cjson.html

- 1,669
- 1
- 12
- 14
-
1I want to remain compatible with the default library provided with 2.6 though, otherwise I'd agree with you. – moinudin Apr 27 '09 at 05:58
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")

- 13,586
- 17
- 76
- 90
json
is a built-in module, you don't need to install it with pip
.

- 379,657
- 97
- 704
- 746

- 253
- 4
- 5
-
1The 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 '20 at 00:52