3
\$\begingroup\$

I have this JSON code that I got from a get request with Azure API. Everything works how I want it but it doesn't look good. There must be an easier way to parse the data.

import json
#GET REQUEST code here, it works now the code below is grabbing the result and loading it.
formatted_response = {'cost': 0, 'timespan': '2020-07-07T03:00:00Z/2020-07-07T03:15:00Z', 'interval': 'TM', 'value': [{'id': '/subscriptions/hjhkjhjljkjknlkjjlkj/Groups/jhkjhjkhjs/providers/Micro/virtualMachines/jhjkhkjhjkhkhljkl/providers/MicInsights/metrics/Percentage CPU', 'type': 'Microsoft.Insights/metrics', 'name': {'value': 'Percentage CPU', 'localizedValue': 'Percentage CPU'}, 'displayDescription': 'The percentage of allocated compute units that are currently in use by the Virtual Machine(s)', 'unit': 'Percent', 'timeseries': [{'metadatavalues': [], 'data': [{'timeStamp': '2020-07-07T03:00:00Z', 'average': 2.4659375}, {'timeStamp': '2020-07-07T03:01:00Z', 'average': 2.974375}, {'timeStamp': '2020-07-07T03:02:00Z', 'average': 2.90265625}, {'timeStamp': '2020-07-07T03:03:00Z', 'average': 1.72484375}, {'timeStamp': '2020-07-07T03:04:00Z', 'average': 1.1275}, {'timeStamp': '2020-07-07T03:05:00Z', 'average': 0.05640625}, {'timeStamp': '2020-07-07T03:06:00Z', 'average': 0.05515625}, {'timeStamp': '2020-07-07T03:07:00Z', 'average': 0.055}, {'timeStamp': '2020-07-07T03:08:00Z', 'average': 0.28765625}, {'timeStamp': '2020-07-07T03:09:00Z', 'average': 0.0546875}, {'timeStamp': '2020-07-07T03:10:00Z', 'average': 0.054375}, {'timeStamp': '2020-07-07T03:11:00Z', 'average': 0.05734375}, {'timeStamp': '2020-07-07T03:12:00Z', 'average': 0.0553125}, {'timeStamp': '2020-07-07T03:13:00Z', 'average': 0.05609375}, {'timeStamp': '2020-07-07T03:14:00Z', 'average': 0.0528125}]}], 'errorCode': 'Success'}], 'namespace': 'Microsoft/virtualMachines', 'resourceregion': 'eastus8'}

p = formatted_response['value']

for i in p:
    for j in i:
        if j == 'timeseries':
            q = i[j]
for l in q:
    for u in l:
        if u =="data":
            g=l['data']

for r in g:
    for s in r:
        if s == "average":
            print(r[s])
$ python testapi.py
2.4659375
2.974375
2.90265625
1.72484375
1.1275
0.05640625
0.05515625
0.055
0.28765625
0.0546875
0.054375
0.05734375
0.0553125
0.05609375
0.0528125
\$\endgroup\$
2
  • \$\begingroup\$ @Peilonrayz Added the output of formatted response above the picture so the code could be tested that way, but now I just put it up top instead so you can run the whole snippet. I just ran it in my terminal and it gave me the output. \$\endgroup\$ Jul 10, 2020 at 15:56
  • 1
    \$\begingroup\$ Ok so formatted_response was the value not formatted_response['value'] like you had originally said. Yes this outputs the same. \$\endgroup\$
    – Peilonrayz
    Jul 10, 2020 at 16:00

1 Answer 1

1
\$\begingroup\$

Json should be put in a separate file.

myfile.json

{   "cost":0,
   "timespan":"2020-07-07T03:00:00Z/2020-07-07T03:15:00Z",
   "interval":"TM",
   "value":[      {         "id":"/subscriptions/hjhkjhjljkjknlkjjlkj/Groups/jhkjhjkhjs/providers/Micro/virtualMachines/jhjkhkjhjkhkhljkl/providers/MicInsights/metrics/Percentage CPU",
         "type":"Microsoft.Insights/metrics",
         "name":{
            "value":"Percentage CPU",
            "localizedValue":"Percentage CPU"
         
},
         "displayDescription":"The percentage of allocated compute units that are currently in use by the Virtual Machine(s)",
         "unit":"Percent",
         "timeseries":[            {               "metadatavalues":[

               
],
               "data":[                  {
                     "timeStamp":"2020-07-07T03:00:00Z",
                     "average":2.4659375
                  
},
                  {
                     "timeStamp":"2020-07-07T03:01:00Z",
                     "average":2.974375
                  
},
                  {
                     "timeStamp":"2020-07-07T03:02:00Z",
                     "average":2.90265625
                  
},
                  {
                     "timeStamp":"2020-07-07T03:03:00Z",
                     "average":1.72484375
                  
},
                  {
                     "timeStamp":"2020-07-07T03:04:00Z",
                     "average":1.1275
                  
},
                  {
                     "timeStamp":"2020-07-07T03:05:00Z",
                     "average":0.05640625
                  
},
                  {
                     "timeStamp":"2020-07-07T03:06:00Z",
                     "average":0.05515625
                  
},
                  {
                     "timeStamp":"2020-07-07T03:07:00Z",
                     "average":0.055
                  
},
                  {
                     "timeStamp":"2020-07-07T03:08:00Z",
                     "average":0.28765625
                  
},
                  {
                     "timeStamp":"2020-07-07T03:09:00Z",
                     "average":0.0546875
                  
},
                  {
                     "timeStamp":"2020-07-07T03:10:00Z",
                     "average":0.054375
                  
},
                  {
                     "timeStamp":"2020-07-07T03:11:00Z",
                     "average":0.05734375
                  
},
                  {
                     "timeStamp":"2020-07-07T03:12:00Z",
                     "average":0.0553125
                  
},
                  {
                     "timeStamp":"2020-07-07T03:13:00Z",
                     "average":0.05609375
                  
},
                  {
                     "timeStamp":"2020-07-07T03:14:00Z",
                     "average":0.0528125
                  
}
               
]
            
}
         
],
         "errorCode":"Success"
      
}
   
],
   "namespace":"Microsoft/virtualMachines",
   "resourceregion":"eastus8"
}

myfile.py

with open("myfile.json") as file_obj:
    formatted_response = json.load(file_obj)

Use descriptive identifiers for variables.

formatted_response_value = formatted_response['value']

Unnecessary double for loop

formatted_response_value = formatted_response['value']
timeseries = formatted_response_value[0]["timeseries"]   #please name something descriptive at all places. 
data = q[0]["data"]
average = list(map(lambda x:x["average"], g))[-1] #or [entry["average"] for entry in g][-1]
\$\endgroup\$
7
  • \$\begingroup\$ Is there a way I can avoid the for loops? it seems excessive. \$\endgroup\$ Jul 10, 2020 at 17:33
  • 1
    \$\begingroup\$ Yep, I was thinking of nested filters. But I 'm trying to understand your code \$\endgroup\$ Jul 10, 2020 at 17:34
  • 1
    \$\begingroup\$ Ask me if you got any confusion without any hesitation. \$\endgroup\$ Jul 10, 2020 at 18:06
  • 1
    \$\begingroup\$ Please upvote the answer if you like it. \$\endgroup\$ Jul 10, 2020 at 18:07
  • 1
    \$\begingroup\$ Thanks:) . Use of map, filter can cut down your loops. \$\endgroup\$ Jul 10, 2020 at 18:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.