2

I'm quite new with JSON and Python and trying to work with complex JSON outputs that I'm getting with GET requests. This is one example of JSON output (this is a small part of it but the principle is the same):

{
  "innerSet": [
    {
      "clusterUID": {
        "id": 3585057579401361143
      },
      "rpasState": [
        {
          "rpaUID": {
            "clusterUID": {
              "id": 3585057579401361143
            },
            "rpaNumber": 1
          },
          "status": "OK",
          "repositoryConnectivityStatus": {
            "accessStatus": "OK",
            "multipathingProblems": false
          },
          "remoteRPAsDataLinkStatus": [
            {
              "JsonSubType": "RPAConnectivityStatus",
              "clusterUID": {
                "id": 2671811049708195677
              },
              "entityType": "RPA",
              "connectivityStatus": "OK",
              "rpaUID": {
                "clusterUID": {
                  "id": 2671811049708195677
                },
                "rpaNumber": 1
              }
            }
          ],
         }
      ]
    }
 ]
}

I trying to find the best way to print a single value. For example, I need the value of "connectivityStatus". Any help will be much appreciated.

I able to pars simple JSON output. I have managed to get the entire innerSet tree:

x = requests.get('website.com)
d = x.json() print (d['innerSet']) 

However, I'not able to go the lower keys. For example, getting the value for "id" key in "clusterUID":

print (d['innerSet']['clusterUID']['id']) 

Results in the following error: TypeError: list indices must be integers, not str

Regards, Yakir.

2
  • have a look at the json module... Commented Aug 31, 2015 at 13:00
  • Write on google: python json and you'll get everything you need. Commented Aug 31, 2015 at 13:03

2 Answers 2

1

You can do this:

import simplejson as json
data = json.loads(s)
print data['innerSet'][0]['rpasState'][0]['remoteRPAsDataLinkStatus'][0]['connectivityStatus']
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! That worked. Can you explain when (and why) the path needed to contain these "[0]"? I want to be able to implement it on the rest of the JSON parsing. Thanks again!
The [0] is there because you access the first element of a list.
the square bracket following "innerSet": [ specifies that innerSet is a list, similarly rpasState, remoteRPAsDataLinkStatus.
why use json library it throw an error?should must use simplejson?
1

For complex JSON, you can user dpath it's like Xpath but on dict.

according to your json you could parse it as:

print(list(dpath.util.search(t, '**/connectivityStatus', yielded=True)))
print(dpath.util.get(t, '/innerSet/0/rpasState/0/remoteRPAsDataLinkStatus/0/connectivityStatus'))

[('innerSet/0/rpasState/0/remoteRPAsDataLinkStatus/0/connectivityStatus', 'OK')]
OK

2 Comments

Thanks you too, worked as well. The first suggestion however, gave all the "connectivityStatus" key it could found (make sense). But the usage of this module is clear and simple. Thanks.
This approach it's suited for complex JSON like REST response.

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.