0

I'm currently working with some JSON data that is presenting a challenge, i need to print the device name in my script next to the latency float, i can easily print the latency float as there is a key:value , however the device name does not sit the same, therefore i cannot figure out how to print this especially as it changes for each API Url i am looping through to retrieve the data

The data i want to print is "DEVICE123-Et10"

See JSON data below,

{
  "notifications": [
    {
      "timestamp": "511513234234",
      "path_elements": [
        "Devices",
        "DEVICE1",
        "versioned-data",
        "connectivityMonitor",
        "status",
        "hostStatus",
        "DEVICE123-Et10",
        "defaultStats"
      ],
      "updates": {
        "httpResponseTime": {
          "key": "httpResponseTime",
          "value": {
            "float": 0
          }
        }
      }
    },
    {
      "timestamp": "15153324243",
      "path_elements": [
        "Devices",
        "DEVICE1",
        "versioned-data",
        "connectivityMonitor",
        "status",
        "hostStatus",
        "DEVICE123-Et10",
        "defaultStats"
      ],
      "updates": {
        "packetLoss": {
          "key": "packetLoss",
          "value": {
            "int": 0
          }
        }
      }
    },
    {
      "timestamp": "151522324234",
      "path_elements": [
        "Devices",
        "DEVICE1",
        "versioned-data",
        "connectivityMonitor",
        "status",
        "hostStatus",
        "DEVICE123-Et10",
        "defaultStats"
      ],
      "updates": {
        "latency": {
          "key": "latency",
          "value": {
            "float": 0.238756565643454
          }
        }
      }
    },
    {
      "timestamp": "158056745645645",
      "path_elements": [
        "Devices",
        "DEVICE1",
        "versioned-data",
        "connectivityMonitor",
        "status",
        "hostStatus",
        "DEVICE123-Et10",
        "defaultStats"
      ],
      "updates": {
        "jitter": {
          "key": "jitter",
          "value": {
            "float": 0.03500000213213
          }
        }
      }
    }
  ]
}

Current code i am using to loop through my URL list and get the latency:

     jsonrequest = requests.get(url, cookies=cookies, verify=False).json()
     try:
         print(jsonrequest['notifications'][2]['updates']['latency']['value']['float'])
     except KeyError:
         print(jsonrequest['notifications'][1]['updates']['latency']['value']['float'])```

1
  • 1
    Since the device name is always second-last in the list, just access list by using the key "path_elements" and then get the second last element. Something like json_data["path_elements"][-2] Commented Apr 29, 2020 at 8:47

1 Answer 1

0

I went ahead and wrote a script to do what you wanted. It loops through all the notifications until a "latency" update is found. Then it takes the second-to-last item from the list, since it's always second to last.

import json
import requests

data = requests.get(url, cookies=cookies, verify=False).json()
notifications = data["notifications"]
for notification in notifications:
    if notification["updates"].get("latency"):
        latency = notification["updates"]["latency"]["value"]["float"]
        name = notification["path_elements"][-2]
        print(name, latency)

Sign up to request clarification or add additional context in comments.

7 Comments

The above example does not seem to work, my current code which works without the device name looks as follows: ` jsonrequest = requests.get(url, cookies=cookies, verify=False).json() try: print(jsonrequest['notifications'][2]['updates']['latency']['value']['float']) except KeyError: print(jsonrequest['notifications'][1]['updates']['latency']['value']['float'])`
When you say it doesn't work, what is the error? What's wrong with it?
Maybe i am using the incorrect syntax however i am unsure how to loop through each url individually with your code, as my JSON data is coming from a list of URLs and not a specific file. Therefore i am using for url in URLs: already
It should still work. I've updated it to make it more clear. What is the error you get with my solution?
TypeError: the JSON object must be str, bytes or bytearray, not dict
|

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.