Extracting average time series 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 ==&quot;data&quot;:
            g=l['data']

for r in g:
    for s in r:
        if s == &quot;average&quot;:
            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
Json should be put in a separate file.
myfile.json
{   &quot;cost&quot;:0,
   &quot;timespan&quot;:&quot;2020-07-07T03:00:00Z/2020-07-07T03:15:00Z&quot;,
   &quot;interval&quot;:&quot;TM&quot;,
   &quot;value&quot;:[      {         &quot;id&quot;:&quot;/subscriptions/hjhkjhjljkjknlkjjlkj/Groups/jhkjhjkhjs/providers/Micro/virtualMachines/jhjkhkjhjkhkhljkl/providers/MicInsights/metrics/Percentage CPU&quot;,
         &quot;type&quot;:&quot;Microsoft.Insights/metrics&quot;,
         &quot;name&quot;:{
            &quot;value&quot;:&quot;Percentage CPU&quot;,
            &quot;localizedValue&quot;:&quot;Percentage CPU&quot;
         
},
         &quot;displayDescription&quot;:&quot;The percentage of allocated compute units that are currently in use by the Virtual Machine(s)&quot;,
         &quot;unit&quot;:&quot;Percent&quot;,
         &quot;timeseries&quot;:[            {               &quot;metadatavalues&quot;:[

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

myfile.py
with open(&quot;myfile.json&quot;) 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][&quot;timeseries&quot;]   #please name something descriptive at all places. 
data = q[0][&quot;data&quot;]
average = list(map(lambda x:x[&quot;average&quot;], g))[-1] #or [entry[&quot;average&quot;] for entry in g][-1]