JSON

Tip

python 2.6 included the simplejson module, but it was renamed to json.

File

Read

import json

with open("out.json", "r") as f:
    data = json.load(f)

Write

import json

with open("out.json", "w") as f:
    json.dump(data, f, indent=4)

Legacy

Install

Note

No need to install simplejson for python 2.6 and above. Just use json instead.

From json and simplejson module differences in Python:

json is simplejson, added to the stdlib. Since json was only added in 2.6, simplejson has the advantage of working on more python versions (2.4+, rather than 2.6+). Also, simplejson is updated more frequently than Python is, so if you need (or want) the latest version, it’s best to use simplejson itself, if possible.

A good practice, in my opinion, is to use one or the other as a fallback.

try: import simplejson as json
except ImportError: import json

jython

Install Easy Install:

jython \tools\jython2.5.1\bin\easy_install simplejson

Windows

Download simplejson-1.7.3.tar.gz from http://cheeseshop.python.org/pypi/simplejson

Build and Install: Extract the file to a temporary folder. Open a Visual Studio 2005 Command Prompt, change into the temporary folder and run:

python setup.py install

Sample

Dump

import simplejson # or json for python 2.6
d = {'name': 'Sam', 'age': 47}

simplejson.dumps(d)
>>> '{"age": 47, "name": "Sam"}'

Dump and then load all class attributes:

import simplejson # or json for python 2.6

class client:
    def __init__(self, name = None, address = None, age = None):
        self.name = name
        self.address = address
        self.age = age

    def __str__(self):
        return self.name + ', ' + self.address + ', ' + str(self.age)

if __name__ == '__main__':
    c = client('Patrick', '2 Moorview', 43)
    j = simplejson.dumps(c.__dict__)
    print 'JSON:'
    print j
    c = client()
    c.__dict__ = simplejson.loads(j)
    print 'Loads:'
    print c

Joy of Python: Classes and Dictionaries, all the attributes in a class are already in a __dict__

Dump to a file:

import simplejson # or json for python 2.6
f = open('out.json', 'wb')
data = # create a list/dictionary etc...
simplejson.dump(data, f, indent=4)

Load

import simplejson # or json for python 2.6
s = '{"name":"Exbourne","id":2}'

simplejson.loads(s)
>>> {'name': 'Exbourne', 'id': 2}