-1

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'}]
Giacomo
  • 344
  • 3
  • 11
JR21
  • 33
  • 5
  • Also, what is this "u" that is being displayed for every key-value pair? – JR21 Nov 23 '21 at 13:37
  • you can use `json` [module](https://docs.python.org/3/library/json.html) for that, then no `for` will be needed. [link](https://stackoverflow.com/questions/19483351/converting-json-string-to-dictionary-not-list). about the 'u' issue - which python version do you use? since this was common in python2 [link2](https://stackoverflow.com/questions/956867/how-to-get-string-objects-instead-of-unicode-from-json) – jsofri Nov 23 '21 at 13:39
  • It's python2.7 on a win server. To upgrade, the server has no internet access and there are a lot of approvals and access requests that will take forever. So I have to work with what I have. – JR21 Nov 23 '21 at 16:10
  • then this requires decoding unicode to strings. see [similar issue](https://stackoverflow.com/questions/36954511/convert-unicode-json-to-normal-json-in-python) – jsofri Nov 23 '21 at 16:16

3 Answers3

0

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

Abhishek
  • 423
  • 6
  • 12
0

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)
JR21
  • 33
  • 5
0
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}

A D
  • 585
  • 1
  • 7
  • 16
  • I copy pasted this code and all I changed was print(min(l)) from print(*l) and it worked. Now I have to integrate this with my code, because values in "x" are returned from an API call, and just having it directly in "x_json_list = json.dumps(x)" is throwing errors. I will try to fix it. – JR21 Nov 24 '21 at 13:47
  • @JR21 Does your API call returns an object, in which you have a list of obj like `x` inside that? If yes, `x_json_list = json.loads(json.dumps(x)); l = [dd["Active Tunnels"] for el in x_json_list for json_like_el in el.values() for dd in json_like_el]; print(min(l))`. I used `;` to seperate new line. – A D Nov 24 '21 at 19:41