Need help to draw JSON values ​​using Python, get TypeError

advertisements

Here's an example of the JSON I'm pulling from a URL:

[
{
    "externalModelId": "500A000000RQOwnIAH",
    "resource": {
        "account": {
            "externalModelId": "001A000001EucpoIAB",
            "resource": {
                "accountName": "Foobar",
                "accountNumber": 1234567,
            },
            "resourceReliability": "Fresh"
        },
        "caseNumber": 1234567,
        "created": "2015-06-12T19:06:22.000Z",
        "createdBy": {
            "externalModelId": "005A0000005mhdXIAQ",
            "resourceReliability": "Fresh"
        },
        "description": "Example description",
        "hoursInCurrentStatus": 406,
        "internalPriority": "3 (Normal)",
        "lastModified": "2015-06-22T14:08:18.000Z",
        "owner": {
            "externalModelId": "005A0000001sKDzIAM",
            "resourceReliability": "Fresh"
        },
        "product": {
            "resource": {
                "line": {
                    "externalModelId": 21118,
                    "resource": {
                        "name": null
                    },
                    "resourceReliability": "Fresh"
                },
                "version": {
                    "externalModelId": 21988,
                    "resource": {
                        "name": "1.2"
                    },
                    "resourceReliability": "Fresh"
                }
            },
            "resourceReliability": "Fresh"
        },
        "resourceCount": 0,
        "sbrs": [
            "Value"
        ],
        "sbt": 139,
        "severity": "4 (Low)",
        "status": "Status Example",
        "subject": "Subject Example",
        "tags": [
            "br",
            "fs"
        ],
        "targetDate": "2015-07-15T17:46:48.000Z",
        "type": "Feature"
    },
    "resourceReliability": "Fresh"
},

I'm interested in pulling the following values from it:

  • caseNumber
  • subject
  • severity
  • sbt
  • sbrs
  • status

The code I currently have is:

#!/usr/bin/env python
import sys
import requests
import json
import os

# Setup
username = "XXX"
password = "XXX"
accountid = "12345"

# Formulate the string and then capture the output
url = "http://XXX{0}XXX{1}XXXXX".format(accountid, filedtime)
r = requests.get(url, auth=(username, password))
parsed = json.loads(r.text)
parent = parsed['resource']

# Using json_string for testing
#json_string = json.dumps(parsed, indent=4, sort_keys=True)
#print json_string

for item in parent:
    print item['caseNumber']
    print item['subject']
    print item['severity']
    print item['sbt']
    print item['sbrs']
    print item['status']

The code outputs a TypeError:

Traceback (most recent call last):
  File "./newcase-notify.py", line 31, in <module>
    parent = parsed['resource']
TypeError: list indices must be integers, not str

I've tried specifying something like:

parent = parsed['resource'][0]['type']

but that doesn't work. I think I'm confused at this point. If I don't specify a parent and simply iterate through 'parsed' like:

for item in parsed:
    print item['caseNumber']
    print item['subject']
    print item['severity']
    print item['sbt']
    print item['sbrs']
    print item['status']

I get KeyError's again.

My Question:

Given the information provided, how can I pull the above mentioned values from my JSON object?


I solved this by removing:

parent = parsed['resource']

and using:

for item in parsed:
    print item['resource']['caseNumber']
    print item['resource']['subject']
    print item['resource']['severity']

etc.