0

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.

3
  • 4
    "context" is list, not dict. Commented Aug 13, 2013 at 17:11
  • 1
    There is no health-status key anywhere in the structure, only health-state, in a deeply nested object. Commented Aug 13, 2013 at 17:15
  • xcoder, thanks not sure how i didnt spot that :D Martijn Pieters, thats a miss type on my part. Thanks Commented Aug 13, 2013 at 17:21

3 Answers 3

3

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']
Sign up to request clarification or add additional context in comments.

Comments

1

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'

Comments

1

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']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.