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?

Tim
  • 41,901
  • 18
  • 127
  • 145
moinudin
  • 134,091
  • 45
  • 190
  • 216
  • https://stackoverflow.com/questions/41466431/pip-install-json-fails-on-ubuntu/41466452#41466452 – cs95 Jun 25 '19 at 19:03

6 Answers6

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 
Community
  • 1
  • 1
scottynomad
  • 1,351
  • 11
  • 9
  • 1
    Thanks! 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
  • 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 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
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 

pkoch
  • 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
  • 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 '12 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 http://pypi.python.org/pypi/simplejson/. – schlamar Mar 14 '12 at 07: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...

Matt Billenstein
  • 678
  • 1
  • 4
  • 7
1

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

David
  • 1,669
  • 1
  • 12
  • 14
  • 1
    I 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
-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")
KatieK
  • 13,586
  • 17
  • 76
  • 90
-1

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

cs95
  • 379,657
  • 97
  • 704
  • 746
  • 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 '20 at 00:52