Home > Back-end >  How to break for loop after got two matching from json response
How to break for loop after got two matching from json response

Time:06-03

I just wondering is it possible to break the loop for after getting 1 or 2 results and asign it variables with same condition.

import json

lookup_dt = "2022-03-09"

r = '{"DBClusterSnapshots": [{"DBClusterSnapshotIdentifier": "snap-1", "DBClusterIdentifier": "aurora-cluster", "SnapshotCreateTime": "2022-03-09", "Engine": "aurora-postgresql", "EngineVersion": "12.7", "SnapshotType": "manual"}, {"DBClusterSnapshotIdentifier": "snap-2", "DBClusterIdentifier": "aurora-cluster", "SnapshotCreateTime": "2022-03-09", "Engine": "aurora-postgresql", "EngineVersion": "12.7", "SnapshotType": "manual"}, {"DBClusterSnapshotIdentifier": "snap-3", "DBClusterIdentifier": "aurora-cluster", "SnapshotCreateTime": "2022-03-09", "Engine": "aurora-postgresql", "EngineVersion": "12.7", "SnapshotType": "manual"}]}'

data = json.loads(r)

for i in data["DBClusterSnapshots"]:
    #if i["SnapshotCreateTime"].strftime("%Y-%m-%d") == lookup_dt:
    if i["SnapshotCreateTime"] == lookup_dt:
        snap1 = i["DBClusterSnapshotIdentifier"]
        snap2 = i["DBClusterSnapshotIdentifier"] 
        break
print(snap1)
print(snap2)

I would expecting output like:

python3.9 ./json_test.py

snap-1
snap-2

Is this possible or doesn't make any senesce of it?

CodePudding user response:

You can use a simple list comprehension to put all the snapshot names into a list:

import json

lookup_dt = "2022-03-09"
r = '{"DBClusterSnapshots": [{"DBClusterSnapshotIdentifier": "snap-1", "DBClusterIdentifier": "aurora-cluster", "SnapshotCreateTime": "2022-03-09", "Engine": "aurora-postgresql", "EngineVersion": "12.7", "SnapshotType": "manual"}, {"DBClusterSnapshotIdentifier": "snap-2", "DBClusterIdentifier": "aurora-cluster", "SnapshotCreateTime": "2022-03-09", "Engine": "aurora-postgresql", "EngineVersion": "12.7", "SnapshotType": "manual"}, {"DBClusterSnapshotIdentifier": "snap-3", "DBClusterIdentifier": "aurora-cluster", "SnapshotCreateTime": "2022-03-09", "Engine": "aurora-postgresql", "EngineVersion": "12.7", "SnapshotType": "manual"}]}'
data = json.loads(r)

snaps = [
    d["DBClusterSnapshotIdentifier"]
    for d in data["DBClusterSnapshots"]
    if d["SnapshotCreateTime"] == lookup_dt
]
print(snaps)  # ['snap-1', 'snap-2', 'snap-3']

If you want to assign the first two of those to the variables snap1 and snap2 you could do:

snap1, snap2 = snaps[:2]

but in practice you'll probably find it easier to just use that snaps list as-is.

(edit) Per comments, if you want a dict of snapshot ID to creation time, that would be:

snap_times = {
    d["DBClusterSnapshotIdentifier"]: d["SnapshotCreateTime"]
    for d in data["DBClusterSnapshots"]
    if d["SnapshotCreateTime"] == lookup_dt
}

print(snap_times)
# {'snap-1': '2022-03-09', 'snap-2': '2022-03-09', 'snap-3': '2022-03-09'}

Note that since we filtered using lookup_dt, all the creation times will equal lookup_dt. If you don't want that, just remove the if d["SnapshotCreateTime"] == lookup_dt line.

  • Related