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
Peilonrayz
44.4k7 gold badges80 silver badges157 bronze badges
asked Jul 10, 2020 at 15:39
\$\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\$ Commented 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\$ Commented 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月07日T03: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月07日T03:00:00Z",
 "average":2.4659375
 
},
 {
 "timeStamp":"2020年07月07日T03:01:00Z",
 "average":2.974375
 
},
 {
 "timeStamp":"2020年07月07日T03:02:00Z",
 "average":2.90265625
 
},
 {
 "timeStamp":"2020年07月07日T03:03:00Z",
 "average":1.72484375
 
},
 {
 "timeStamp":"2020年07月07日T03:04:00Z",
 "average":1.1275
 
},
 {
 "timeStamp":"2020年07月07日T03:05:00Z",
 "average":0.05640625
 
},
 {
 "timeStamp":"2020年07月07日T03:06:00Z",
 "average":0.05515625
 
},
 {
 "timeStamp":"2020年07月07日T03:07:00Z",
 "average":0.055
 
},
 {
 "timeStamp":"2020年07月07日T03:08:00Z",
 "average":0.28765625
 
},
 {
 "timeStamp":"2020年07月07日T03:09:00Z",
 "average":0.0546875
 
},
 {
 "timeStamp":"2020年07月07日T03:10:00Z",
 "average":0.054375
 
},
 {
 "timeStamp":"2020年07月07日T03:11:00Z",
 "average":0.05734375
 
},
 {
 "timeStamp":"2020年07月07日T03:12:00Z",
 "average":0.0553125
 
},
 {
 "timeStamp":"2020年07月07日T03:13:00Z",
 "average":0.05609375
 
},
 {
 "timeStamp":"2020年07月07日T03: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]
answered Jul 10, 2020 at 17:10
\$\endgroup\$
7
  • \$\begingroup\$ Is there a way I can avoid the for loops? it seems excessive. \$\endgroup\$ Commented Jul 10, 2020 at 17:33
  • 1
    \$\begingroup\$ Yep, I was thinking of nested filters. But I 'm trying to understand your code \$\endgroup\$ Commented Jul 10, 2020 at 17:34
  • 1
    \$\begingroup\$ Ask me if you got any confusion without any hesitation. \$\endgroup\$ Commented Jul 10, 2020 at 18:06
  • 1
    \$\begingroup\$ Please upvote the answer if you like it. \$\endgroup\$ Commented Jul 10, 2020 at 18:07
  • 1
    \$\begingroup\$ Thanks:) . Use of map, filter can cut down your loops. \$\endgroup\$ Commented Jul 10, 2020 at 18:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.