Parsing JSON in Python with HTTP

To parse JSON data in Python is a pretty similar process; however, in this case, our ElementTree library isn't needed, since that only works with XML. We need a library designed to parse JSON using Python. Luckily, the Python library creators already have a library for us, simply called json.

Let's build an example similar to our XML code using the json import; of course, we need to use a different data source since we won't be working in XML. One thing we may note is that there aren't many public JSON feeds to use, many of which require using a code that gives a developer permission to generate a JSON feed through a developer API, such as Twitter's JSON API. To avoid this, we will use a sample URL from Google's Books API, which will show demo data of Pride and Prejudice, Jane Austen. We can view the JSON (or download the file), by typing in the following URL:

https://www.googleapis.com/books/v1/volumes/s1gVAAAAYAAJ

Note

Notice the API uses HTTPS, many JSON APIs are moving to secure methods of transmitting data, so be sure to include the secure in HTTP, with HTTPS.

Let's take a look at the JSON output:

{
 "kind": "books#volume",
 "id": "s1gVAAAAYAAJ",
 "etag": "yMBMZ85ENrc",
 "selfLink": "https://www.googleapis.com/books/v1/volumes/s1gVAAAAYAAJ",
 "volumeInfo": {
  "title": "Pride and Prejudice",
  "authors": [
   "Jane Austen"
  ],
  "publisher": "C. Scribner's Sons",
  "publishedDate": "1918",
  "description": "Austen's most celebrated novel tells the story of Elizabeth Bennet, a bright, lively young woman with four sisters, and a mother determined to marry them to wealthy men. At a party near the Bennets' home in the English countryside, Elizabeth meets the wealthy, proud Fitzwilliam Darcy. Elizabeth initially finds Darcy haughty and intolerable, but circumstances continue to unite the pair. Mr. Darcy finds himself captivated by Elizabeth's wit and candor, while her reservations about his character slowly vanish. The story is as much a social critique as it is a love story, and the prose crackles with Austen's wry wit.",
  "readingModes": {
   "text": true,
   "image": true
  },
  "pageCount": 401,
  "printedPageCount": 448,
  "dimensions": {
   "height": "18.00 cm"
  },
  "printType": "BOOK",
  "averageRating": 4.0,
  "ratingsCount": 433,
  "contentVersion": "1.1.5.0.full.3",
  "imageLinks": {
   "smallThumbnail": "http://bks8.books.google.com/books?id=s1gVAAAAYAAJ&printsec=frontcover&img=1&zoom=5&edge=curl&imgtk=AFLRE73F8btNqKpVjGX6q7V3XS77QA2PftQUxcEbU3T3njKNxezDql_KgVkofGxCPD3zG1yq39u0XI8s4wjrqFahrWQ-5Epbwfzfkoahl12bMQih5szbaOw&source=gbs_api",
   "thumbnail": "http://bks8.books.google.com/books?id=s1gVAAAAYAAJ&printsec=frontcover&img=1&zoom=1&edge=curl&imgtk=AFLRE70tVS8zpcFltWh_7K_5Nh8BYugm2RgBSLg4vr9tKRaZAYoAs64RK9aqfLRECSJq7ATs_j38JRI3D4P48-2g_k4-EY8CRNVReZguZFMk1zaXlzhMNCw&source=gbs_api",
   "small": "http://bks8.books.google.com/books?id=s1gVAAAAYAAJ&printsec=frontcover&img=1&zoom=2&edge=curl&imgtk=AFLRE71qcidjIs37x0jN2dGPstn6u2pgeXGWZpS1ajrGgkGCbed356114HPD5DNxcR5XfJtvU5DKy5odwGgkrwYl9gC9fo3y-GM74ZIR2Dc-BqxoDuUANHg&source=gbs_api",
   "medium": "http://bks8.books.google.com/books?id=s1gVAAAAYAAJ&printsec=frontcover&img=1&zoom=3&edge=curl&imgtk=AFLRE73hIRCiGRbfTb0uNIIXKW4vjrqAnDBSks_ne7_wHx3STluyMa0fsPVptBRW4yNxNKOJWjA4Od5GIbEKytZAR3Nmw_XTmaqjA9CazeaRofqFskVjZP0&source=gbs_api",
   "large": "http://bks8.books.google.com/books?id=s1gVAAAAYAAJ&printsec=frontcover&img=1&zoom=4&edge=curl&imgtk=AFLRE73mlnrDv-rFsL-n2AEKcOODZmtHDHH0QN56oG5wZsy9XdUgXNnJ_SmZ0sHGOxUv4sWK6GnMRjQm2eEwnxIV4dcF9eBhghMcsx-S2DdZoqgopJHk6Ts&source=gbs_api",
   "extraLarge": "http://bks8.books.google.com/books?id=s1gVAAAAYAAJ&printsec=frontcover&img=1&zoom=6&edge=curl&imgtk=AFLRE73KIXHChsznTbrXnXDGVs3SHtYpl8tGncDPX_7GH0gd7sq7SA03aoBR0mDC4-euzb4UCIDiDNLYZUBJwMJxVX_cKG5OAraACPLa2QLDcfVkc1pcbC0&source=gbs_api"
  },
  "language": "en",
  "previewLink": "http://books.google.com/books?id=s1gVAAAAYAAJ&hl=&source=gbs_api",
  "infoLink": "http://books.google.com/books?id=s1gVAAAAYAAJ&hl=&source=gbs_api",
  "canonicalVolumeLink": "http://books.google.com/books/about/Pride_and_Prejudice.html?hl=&id=s1gVAAAAYAAJ"
 },
 "layerInfo": {
  "layers": [
   {
    "layerId": "geo",
    "volumeAnnotationsVersion": "6"
   }
  ]
 },
 "saleInfo": {
  "country": "US",
  "saleability": "FREE",
  "isEbook": true,
  "buyLink": "http://books.google.com/books?id=s1gVAAAAYAAJ&hl=&buy=&source=gbs_api"
 },
 "accessInfo": {
  "country": "US",
  "viewability": "ALL_PAGES",
  "embeddable": true,
  "publicDomain": true,
  "textToSpeechPermission": "ALLOWED",
  "epub": {
   "isAvailable": true,
   "downloadLink": "http://books.google.com/books/download/Pride_and_Prejudice.epub?id=s1gVAAAAYAAJ&hl=&output=epub&source=gbs_api"
  },
  "pdf": {
   "isAvailable": true,
   "downloadLink": "http://books.google.com/books/download/Pride_and_Prejudice.pdf?id=s1gVAAAAYAAJ&hl=&output=pdf&sig=ACfU3U3dQw5JDWdbVgk2VRHyDjVMT4oIaA&source=gbs_api"
  },
  "webReaderLink": "http://books.google.com/books/reader?id=s1gVAAAAYAAJ&hl=&printsec=frontcover&output=reader&source=gbs_api",
  "accessViewStatus": "FULL_PUBLIC_DOMAIN",
  "quoteSharingAllowed": false
 }
}

One downside to JSON is that it can be hard to read complex data. So, if we run across a complex JSON feed, we can visualize it using a JSON Visualizer. Visual Studio includes one with all its editions, and a web search will also show multiple online sites where you can paste JSON and an easy-to-understand data structure will be displayed. Here's an example using http://jsonviewer.stack.hu/ loading our example JSON URL:

Parsing JSON in Python with HTTP

Next, let's reuse some of our existing Python code using our urllib2 library to request our JSON feed, and then we will parse it with the Python's JSON library. Let's parse the volumeInfo node of the book by starting with the JSON (root) node that is followed by volumeInfo as the subnode. Here's our example from the XML section, reworked using JSON to parse all child elements:

# -*- coding: utf-8 -*-

import urllib2
import json
try:
    #Set a URL variable.
    url = 'https://www.googleapis.com/books/v1/volumes/s1gVAAAAYAAJ'
    #Open the file via HTTP.
    response = urllib2.urlopen(url)

    #Read the request as one string.
    bookdata = response.read()

    #Convert the string to a JSON object in Python.
    data = json.loads(bookdata)

    for r in data ['volumeInfo']:
        print r

    #Close our response.
    response.close()

except:
    #If we have an issue show a message and alert the user.
    print('Unable to connect to JSON API...')

Here's our output. It should match the child nodes of volumeInfo, which it does in the output screen, as shown in the following screenshot:

Parsing JSON in Python with HTTP

Well done! Now, let's grab the value for title. Look at the following example and notice we have two brackets: one for volumeInfo and another for title. This is similar to navigating our XML hierarchy:

# -*- coding: utf-8 -*-

import urllib2
import json

try:
    #Set a URL variable.
    url = 'https://www.googleapis.com/books/v1/volumes/s1gVAAAAYAAJ'

    #Open the file via HTTP.
    response = urllib2.urlopen(url)

    #Read the request as one string.
    bookdata = response.read()

    #Convert the string to a JSON object in Python.
    data = json.loads(bookdata)

    print data['volumeInfo']['title']

    #Close our response.
    response.close()

except Exception as e:
    #If we have an issue show a message and alert the user.
    #'Unable to connect to JSON API...'
    print(e)

The following screenshot shows the results of our script:

Parsing JSON in Python with HTTP

As you can see in the preceding screenshot, we return one line with Pride and Prejudice parsed from our JSON data.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.213.238