-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'}]
4
  • Also, what is this "u" that is being displayed for every key-value pair? Commented Nov 23, 2021 at 13:37
  • you can use json module for that, then no for will be needed. link. about the 'u' issue - which python version do you use? since this was common in python2 link2 Commented Nov 23, 2021 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. Commented Nov 23, 2021 at 16:10
  • then this requires decoding unicode to strings. see similar issue Commented Nov 23, 2021 at 16:16

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I will try.
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)

Comments

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}

2 Comments

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 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.

Your Answer

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.