Main Content

Handling JSON in Python (and a csv, marshall and pickle comparison)

Archive - Originally posted on "The Horse's Mouth" - 2013-11-16 16:54:54 - Graham Ellis

JSON - JavaScript Object Notation - has become the data encoding standard of choice for many applications far removed from the "Javascript" of its name. It's a compact, readable format which allows collections within a language (that would be lists and dicts in Python, for example) to be encoded into a text string for file storage or network transmission, and easily decoded later / at the other end.

I wrote a demonstration during last week's Python Programming Course to show how JSON data can be read - from a file, or from a web service, and the data loaded into dicts and / or lists with a single call:
  rawinfo = stream.read()
  info = json.loads(rawinfo)


The data can be outpout just as easily:
  rawoutput = json.dumps(info)
  print rawoutput


You do need to import the json module first:
  import json

Source code - [here].

The example also included writing parts of the same data out to a .csv file. You could write this longhand (as I did [here] on a previous course), or you could use a json writer filter:

  import csv
  
  outstream = open("soumya.csv","w")
  author = csv.writer(outstream)
  for visit in info["acc"]:
    print visit["when"],visit["where"],visit["which"]
    author.writerow([visit["when"],visit["where"],visit["which"]])
  outstream.close()


JSON broadly replaces marshalling of data in Python (see old examples [here] and [here]) - not much of a change as (!) the method names are the same. It also replaces pickling - see the older examples [here] and [here].