By Lobinho


2020-03-26 02:28:23 8 Comments

I want to get only the "dn" and "totalCum" from json below with python. Now "dn" works, but "totalCum" does not work, as I checked that there are files with lines before

response = requests.get('https://{}/api/class/eqptcapacityEntity.json?query-target=self&rsp-subtree-include=stats&rsp-subtree-class=eqptcapacityVlanUsage5min'.format(fqdn), cookies=cookie, verify=False)
response_ep = response.json()
for entity in response_ep["imdata"]:
    print(entity["eqptcapacityEntity"]["attributes"]["dn"])
    print(entity["eqptcapacityEntity"]["children"]["eqptcapacityVlanUsage5min"]["attributes"]["totalCum"])

output:

{
    "imdata": [
        {
            "eqptcapacityEntity": {
                "attributes": {
                    "childAction": "",
                    "dn": "topology/pod-1/node-101/sys/eqptcapacity",
                    "lcOwn": "local",
                    "modTs": "2019-07-24T15:13:35.347-03:00",
                    "monPolDn": "uni/fabric/monfab-default",
                    "name": "",
                    "nameAlias": "",
                    "status": ""
                }
            }
        },
        {
            "eqptcapacityEntity": {
                "attributes": {
                    "childAction": "",
                    "dn": "topology/pod-1/node-102/sys/eqptcapacity",
                    "lcOwn": "local",
                    "modTs": "2019-06-25T04:47:07.829-03:00",
                    "monPolDn": "uni/fabric/monfab-default",
                    "name": "",
                    "nameAlias": "",
                    "status": ""
                }
            }
        },
        {
            "eqptcapacityEntity": {
                "attributes": {
                    "childAction": "",
                    "dn": "topology/pod-2/node-201/sys/eqptcapacity",
                    "lcOwn": "local",
                    "modTs": "2019-06-24T21:50:37.543-03:00",
                    "monPolDn": "uni/fabric/monfab-default",
                    "name": "",
                    "nameAlias": "",
                    "status": ""
                }
            }
        },
        {
            "eqptcapacityEntity": {
                "attributes": {
                    "childAction": "",
                    "dn": "topology/pod-2/node-202/sys/eqptcapacity",
                    "lcOwn": "local",
                    "modTs": "2019-06-25T04:20:19.498-03:00",
                    "monPolDn": "uni/fabric/monfab-default",
                    "name": "",
                    "nameAlias": "",
                    "status": ""
                }
            }
        },
        {
            "eqptcapacityEntity": {
                "attributes": {
                    "childAction": "",
                    "dn": "topology/pod-52/node-555/sys/eqptcapacity",
                    "lcOwn": "local",
                    "modTs": "2019-06-24T21:46:34.791-03:00",
                    "monPolDn": "uni/fabric/monfab-default",
                    "name": "",
                    "nameAlias": "",
                    "status": ""
                },
                "children": [
                    {
                        "eqptcapacityVlanUsage5min": {
                            "attributes": {
                                "childAction": "",
                                "cnt": "18",
                                "lastCollOffset": "180",
                                "modTs": "never",
                                "repIntvEnd": "2020-03-25T22:52:49.339-03:00",
                                "repIntvStart": "2020-03-25T22:49:49.339-03:00",
                                "rn": "CDeqptcapacityVlanUsage5min",
                                "status": "",
                                "totalAvg": "0",
                                "totalBase": "0",
                                "totalCapAvg": "0",
                                "totalCapBase": "0",
                                "totalCapCum": "3960",
                                "totalCapLast": "0",
                                "totalCapMax": "0",
                                "totalCapMin": "0",
                                "totalCapPer": "0",
                                "totalCapRate": "0.000000",
                                "totalCapSpct": "0",
                                "totalCapThr": "",
                                "totalCapTr": "0",
                                "totalCapTrBase": "0",
                                "totalCum": "3215",
                                "totalLast": "0",
                                "totalMax": "0",
                                "totalMin": "0",
                                "totalPer": "0",
                                "totalRate": "0.000000",
                                "totalSpct": "0",
                                "totalThr": "",
                                "totalTr": "0",
                                "totalTrBase": "0"
                            }
                        }
                    }
                ]
            }
        },
        {
            "eqptcapacityEntity": {
                "attributes": {
                    "childAction": "",
                    "dn": "topology/pod53/node-556/sys/eqptcapacity",
                    "lcOwn": "local",
                    "modTs": "2019-06-25T04:16:04.293-03:00",
                    "monPolDn": "uni/fabric/monfab-default",
                    "name": "",
                    "nameAlias": "",
                    "status": ""
                },
                "children": [
                    {
                        "eqptcapacityVlanUsage5min": {
                            "attributes": {
                                "childAction": "",
                                "cnt": "17",
                                "lastCollOffset": "170",
                                "modTs": "never",
                                "repIntvEnd": "2020-03-25T22:52:52.946-03:00",
                                "repIntvStart": "2020-03-25T22:50:02.946-03:00",
                                "rn": "CDeqptcapacityVlanUsage5min",
                                "status": "",
                                "totalAvg": "0",
                                "totalBase": "0",
                                "totalCapAvg": "0",
                                "totalCapBase": "0",
                                "totalCapCum": "3960",
                                "totalCapLast": "0",
                                "totalCapMax": "0",
                                "totalCapMin": "0",
                                "totalCapPer": "0",
                                "totalCapRate": "0.000000",
                                "totalCapSpct": "0",
                                "totalCapThr": "",
                                "totalCapTr": "0",
                                "totalCapTrBase": "0",
                                "totalCum": "3215",
                                "totalLast": "0",
                                "totalMax": "0",
                                "totalMin": "0",
                                "totalPer": "0",
                                "totalRate": "0.000000",
                                "totalSpct": "0",
                                "totalThr": "",
                                "totalTr": "0",
                                "totalTrBase": "0"
                            }
                        }
                    }
                ]
            }
        }
    ],
    "totalCount": "6"
}

command: [email protected]:/home/ubuntu/vscode# python3 testes_get.py topology/pod-1/node-101/sys/eqptcapacity Traceback (most recent call last): File "testes_get.py", line 42, in print(entity["eqptcapacityEntity"]["children"]["eqptcapacityVlanUsage5min"]["attributes"]["totalCum"]) KeyError: 'children'

1 comments

@dspencer 2020-03-26 02:33:30

Since you use response.json(), you have a nested dictionary in response_ep - you can retrieve the keys you're looking for by indexing the returned dictionary as a nested dictionary.

for entity in response_ep["imdata"]:
    print(entity["eqptcapacityEntity"]["attributes"]["dn"])
    print(entity["eqptcapacityEntity"]["children"]["eqptcapacityVlanUsage5min"]["attributes"]["totalCum"])

@Lobinho 2020-03-26 03:21:24

sorry, but I still couldn't, follow the error: python3 testes_get.py Traceback (most recent call last): File "testes_get.py", line 40, in <module> json_dict = json.loads(response_ep) File "/usr/lib/python3.6/json/__init__.py", line 348, in loads 'not {!r}'.format(s.__class__.__name__)) TypeError: the JSON object must be str, bytes or bytearray, not 'dict'

@Lobinho 2020-03-26 03:25:15

testes_get.py: #!/usr/bin/env python3 import requests import json import urllib3 import subprocess import argparse import string response = requests.get('https://{}/api/class/eqptcapacityEntity.json?q‌​uery-target=self&rsp‌​-subtree-include=sta‌​ts&rsp-subtree-class‌​=eqptcapacityVlanUsa‌​ge5min'.format(fqdn)‌​, cookies=cookie, verify=False) response_ep = response.json() json_dict = json.loads(response_ep) for entity in json_dict["imdata"]: print(entity["eqptcapacityEntity"]["attributes"]["dn"]) print(entity["children"]["eqptcapacityVlanUsage5min"]["attri‌​butes"]["totalCum"])

@dspencer 2020-03-26 03:25:56

Since response.json() actually returns a dictionary, you don't need to do the json.loads step - check my updated. response_ep itself is already a nested dictionary object.

@Lobinho 2020-03-26 15:04:08

Now "dn" works, but "totalCum" does not work, because I verified that there are lines before the next dictionary I edited the case for you to check again, please

@Lobinho 2020-03-31 16:16:41

Please, could help me?

@dspencer 2020-04-01 04:12:20

If you still need help with this, I suggest you open a new question and address the reason why it was closed this time.

Related Questions

Sponsored Content

51 Answered Questions

[SOLVED] Can comments be used in JSON?

  • 2008-10-28 20:39:03
  • Michael Gundlach
  • 2204904 View
  • 7438 Score
  • 51 Answer
  • Tags:   json comments

20 Answered Questions

[SOLVED] What are metaclasses in Python?

36 Answered Questions

[SOLVED] How to get the current time in Python

  • 2009-01-06 04:54:23
  • user46646
  • 3222684 View
  • 2784 Score
  • 36 Answer
  • Tags:   python datetime time

25 Answered Questions

[SOLVED] Does Python have a ternary conditional operator?

65 Answered Questions

[SOLVED] Calling an external command from Python

7 Answered Questions

[SOLVED] Why does Google prepend while(1); to their JSON responses?

10 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

23 Answered Questions

[SOLVED] How do I POST JSON data with cURL?

35 Answered Questions

[SOLVED] What is the correct JSON content type?

54 Answered Questions

[SOLVED] How can I pretty-print JSON in a shell script?

Sponsored Content