Steve
Steve

Reputation: 401

Unable to pull data from json using python

I have the following json

{
    "response": {
        "message": null,
        "exception": null,
        "context": [
            {
                "headers": null,
                "name": "aname",
                "children": [
                    {
                        "type": "cluster-connectivity",
                        "name": "cluster-connectivity"
                    },
                    {
                        "type": "consistency-groups",
                        "name": "consistency-groups"
                    },
                    {
                        "type": "devices",
                        "name": "devices"
                    },
                    {
                        "type": "exports",
                        "name": "exports"
                    },
                    {
                        "type": "storage-elements",
                        "name": "storage-elements"
                    },
                    {
                        "type": "system-volumes",
                        "name": "system-volumes"
                    },
                    {
                        "type": "uninterruptible-power-supplies",
                        "name": "uninterruptible-power-supplies"
                    },
                    {
                        "type": "virtual-volumes",
                        "name": "virtual-volumes"
                    }
                ],
                "parent": "/clusters",
                "attributes": [
                    {
                        "value": "true",
                        "name": "allow-auto-join"
                    },
                    {
                        "value": "0",
                        "name": "auto-expel-count"
                    },
                    {
                        "value": "0",
                        "name": "auto-expel-period"
                    },
                    {
                        "value": "0",
                        "name": "auto-join-delay"
                    },
                    {
                        "value": "1",
                        "name": "cluster-id"
                    },
                    {
                        "value": "true",
                        "name": "connected"
                    },
                    {
                        "value": "synchronous",
                        "name": "default-cache-mode"
                    },
                    {
                        "value": "true",
                        "name": "default-caw-template"
                    },
                    {
                        "value": "blah",
                        "name": "default-director"
                    },
                    {
                        "value": [
                            "blah",
                            "blah"
                        ],
                        "name": "director-names"
                    },
                    {
                        "value": [

                        ],
                        "name": "health-indications"
                    },
                    {
                        "value": "ok",
                        "name": "health-state"
                    },
                    {
                        "value": "1",
                        "name": "island-id"
                    },
                    {
                        "value": "blah",
                        "name": "name"
                    },
                    {
                        "value": "ok",
                        "name": "operational-status"
                    },
                    {
                        "value": [

                        ],
                        "name": "transition-indications"
                    },
                    {
                        "value": [

                        ],
                        "name": "transition-progress"
                    }
                ],
                "type": "cluster"
            }
        ],
        "custom-data": null
    }
}

which im trying to parse using the json module in python. I am only intrested in getting the following information out of it.

Name Value operational-status Value health-state Value

Here is what i have tried. in the below script data is the json returned from a webpage

json = json.loads(data)
healthstate= json['response']['context']['operational-status']
operationalstatus = json['response']['context']['health-status']

Unfortunately i think i must be missing something as the above results in an error that indexes must be integers not string.

if I try

healthstate= json['response'][0]

it errors saying index 0 is out of range.

Any help would be gratefully received.

Upvotes: 0

Views: 157

Answers (3)

sedavidw
sedavidw

Reputation: 11691

json['reponse']['context'] is a list, not a dict. The structure is not exactly what you think it is.

For example, the only "operational status" I see in there can be read with the following:

json['response']['context'][0]['attributes'][0]['operational-status']

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1121486

json['response']['context'] is a list, so that object requires you to use integer indices.

Each item in that list is itself a dictionary again. In this case there is only one such item.

To get all "name": "health-state" dictionaries out of that structure you'd need to do a little more processing:

[attr['value'] for attr in json['response']['context'][0]['attributes'] if attr['name'] ==  'health-state']

would give you a list of of matching values for health-state in the first context.

Demo:

>>> [attr['value'] for attr in json['response']['context'][0]['attributes'] if attr['name'] ==  'health-state']
[u'ok']

Upvotes: 3

Viktor Kerkez
Viktor Kerkez

Reputation: 46566

You have to follow the data structure. It's best to interactively manipulate the data and check what every item is. If it's a list you'll have to index it positionally or iterate through it and check the values. If it's a dict you'll have to index it by it's keys. For example here is a function that get's the context and then iterates through it's attributes checking for a particular name.

def get_attribute(data, attribute):
    for attrib in data['response']['context'][0]['attributes']:
        if attrib['name'] == attribute:
            return attrib['value']
    return 'Not Found'

>>> data = json.loads(s)
>>> get_attribute(data, 'operational-status')
u'ok'
>>> get_attribute(data, 'health-state')
u'ok'

Upvotes: 1

Related Questions