python API calls json response - lowest value

91 Views Asked by At

I'm doing an API call and getting the below output. But what I'm actually looking for is only the lowest value for 'Active Tunnels' to be displayed. I know "for" loop is the answer but I've tried so many things in the past 5 hours and got no where close to my goal. Please help me.

{u'histdata': [{u'Active Tunnels': 378.0,
                u'Utilization': 2.0,
                u'coverage': u'100 %',
                u'datetime': u'22/11/2021 16:00:49'},
               {u'Active Tunnels': 377.0,
                u'Utilization': 2.0,
                u'coverage': u'100 %',
                u'datetime': u'22/11/2021 16:01:49'},
               {u'Active Tunnels': 376.0,
                u'Utilization': 2.0,
                u'coverage': u'100 %',
                u'datetime': u'22/11/2021 16:02:49'},
               {u'Active Tunnels': 374.0,
                u'Utilization': 2.0,
                u'coverage': u'100 %',
                u'datetime': u'22/11/2021 16:03:49'}]
3

There are 3 best solutions below

1
Abhishek On

You can try this, FYI it's not the optimal solution but i think it will solve the issue , u is for Unicode This will give you the dict having lowest value for Active Tunnels, you can easily get the value for that key

d = {u'histdata': [{u'Active Tunnels': 378.0, u'Utilization': 2.0, u'coverage': u'100 %', u'datetime': u'22/11/2021 16:00:49'}, {u'Active Tunnels': 377.0, u'Utilization': 2.0, u'coverage': u'100 %', u'datetime': u'22/11/2021 16:01:49'}, {u'Active Tunnels': 376.0, u'Utilization': 2.0, u'coverage': u'100 %', u'datetime': u'22/11/2021 16:02:49'}, {u'Active Tunnels': 374.0, u'Utilization': 2.0, u'coverage': u'100 %', u'datetime': u'22/11/2021 16:03:49'}]}

lowest = []
for line in d.get('histdata'):
    low = float(line.get('Active Tunnels'))
    if len(lowest) == 0:
        lowest = [line]
    else:
        if lowest[0].get('Active Tunnels')  > low:
            lowest = [line]
        else:
            pass

print(lowest)
# use this for lowest value
# print(lowest[0].get('Active Tunnels'))

[{'Active Tunnels': 374.0, 'Utilization': 2.0, 'coverage': '100 %', 'datetime': '22/11/2021 16:03:49'}]

And you are missing } at end in your data

0
JR21 On

In the meantime, I was able to isolate only the values for 'Active Tunnels' and print them in integer format. I'm just one step close now i.e. get the lowest value

data=r.json()
print(data['histdata'])
for vpn in data['histdata']:
    tunnel = int(vpn['Active Tunnels'])
    print(tunnel)
2
A D On
import json

x = {
    u"histdata": [
        {
            u"Active Tunnels": 378.0,
            u"Utilization": 2.0,
            u"coverage": u"100 %",
            u"datetime": u"22/11/2021 16:00:49",
        },
        {
            u"Active Tunnels": 377.0,
            u"Utilization": 2.0,
            u"coverage": u"100 %",
            u"datetime": u"22/11/2021 16:01:49",
        },
        {
            u"Active Tunnels": 376.0,
            u"Utilization": 2.0,
            u"coverage": u"100 %",
            u"datetime": u"22/11/2021 16:02:49",
        },
        {
            u"Active Tunnels": 374.0,
            u"Utilization": 2.0,
            u"coverage": u"100 %",
            u"datetime": u"22/11/2021 16:03:49",
        },
    ]
}

x_json_list = json.dumps(x)
d = json.loads(x_json_list)


l = sorted(
    [{"Active Tunnels": dd["Active Tunnels"]} for zz in d.values() for dd in zz],
    key=lambda d: d["Active Tunnels"],
)
print(*l)

# {'Active Tunnels': 374.0} {'Active Tunnels': 376.0} {'Active Tunnels': 377.0} {'Active Tunnels': 378.0}