\$\begingroup\$
\$\endgroup\$
2
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
asked Jul 10, 2020 at 15:39
1 Answer 1
\$\begingroup\$
\$\endgroup\$
7
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
-
\$\begingroup\$ Is there a way I can avoid the for loops? it seems excessive. \$\endgroup\$Colorful Codes– Colorful Codes2020年07月10日 17:33:21 +00:00Commented Jul 10, 2020 at 17:33
-
1\$\begingroup\$ Yep, I was thinking of nested filters. But I 'm trying to understand your code \$\endgroup\$Vishesh Mangla– Vishesh Mangla2020年07月10日 17:34:06 +00:00Commented Jul 10, 2020 at 17:34
-
1\$\begingroup\$ Ask me if you got any confusion without any hesitation. \$\endgroup\$Vishesh Mangla– Vishesh Mangla2020年07月10日 18:06:34 +00:00Commented Jul 10, 2020 at 18:06
-
1\$\begingroup\$ Please upvote the answer if you like it. \$\endgroup\$Vishesh Mangla– Vishesh Mangla2020年07月10日 18:07:13 +00:00Commented Jul 10, 2020 at 18:07
-
1\$\begingroup\$ Thanks:) . Use of map, filter can cut down your loops. \$\endgroup\$Vishesh Mangla– Vishesh Mangla2020年07月10日 18:13:00 +00:00Commented Jul 10, 2020 at 18:13
lang-py
formatted_response
was the value notformatted_response['value']
like you had originally said. Yes this outputs the same. \$\endgroup\$