Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find the minimum in JSON in Python

There is a part of a JSON file:

{
    "payload": {
        "orders": [
            {
                "quantity": 1,
                "platinum": 4,
                "visible": true,
                "order_type": "sell",
                "user": {
                    "reputation": 5,
                    "region": "en",
                    "last_seen": "2022-11-17T08:15:43.360+00:00",
                    "ingame_name": "Noxxat",
                    "id": "5b50d73859d885026b523cd1",
                    "avatar": null,
                    "status": "offline"
                },
                "platform": "pc",
                "region": "en",
                "creation_date": "2020-09-04T15:30:41.000+00:00",
                "last_update": "2021-11-19T09:41:43.000+00:00",
                "id": "5f525da1c98cd000d7513813"
            },
            {
                "order_type": "sell",
                "visible": true,
                "quantity": 2,
                "platinum": 6,
                "user": {
                    "reputation": 3,
                    "region": "en",
                    "last_seen": "2022-11-18T14:22:53.023+00:00",
                    "ingame_name": "Dhatman",
                    "id": "5b79921649262103f74b6585",
                    "avatar": null,
                    "status": "offline"
                },
                "platform": "pc",
                "region": "en",
                "creation_date": "2020-11-06T10:32:32.000+00:00",
                "last_update": "2022-10-11T16:51:55.000+00:00",
                "id": "5fa526406ff3660486ef556c"
            },
            {
                "quantity": 1,
                "visible": true,
                "platinum": 5,
                "order_type": "sell",
                "user": {
                    "reputation": 4,
                    "region": "en",
                    "last_seen": "2022-11-18T18:31:49.199+00:00",
                    "ingame_name": "TheronGuardxx",
                    "avatar": "user/avatar/5e235e94ab7656047a86f70c.png?7b1e90d474a62c6ba3c2d3ef06aed927",
                    "id": "5e235e94ab7656047a86f70c",
                    "status": "offline"
                },
                "platform": "pc",
                "region": "en",
                "creation_date": "2020-12-17T22:46:57.000+00:00",
                "last_update": "2022-10-15T23:37:01.000+00:00",
                "id": "5fdbdfe13e8c4f017f5e3352"
            }
        ]
    }
}

How to find the minimum amount of platinum in this file?
As I understand it, I need to make a loop that will go through the entire file and assign a new value to the variable min if the current amount of platinum is less than the amount currently written in min.

But what should the code look like?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

At the moment I have written a block that finds the amount of platinum, the seller’s alias and the number of items from the last element of the JSON-file.

num = 1
flagSell = 0

while flagSell == 0:
    if r_json["payload"]["orders"][len(r_json["payload"]["orders"]) - num]["user"]['status'] == 'ingame':
        if r_json["payload"]["orders"][len(r_json["payload"]["orders"]) - num]["region"] == 'en':
            if r_json["payload"]["orders"][len(r_json["payload"]["orders"]) - num]["order_type"] == 'sell':
                min = r_json["payload"]["orders"][len(r_json["payload"]["orders"]) - num]["platinum"]
                author = r_json["payload"]["orders"][len(r_json["payload"]["orders"]) - num]["user"]["ingame_name"]
                quantity = r_json["payload"]["orders"][len(r_json["payload"]["orders"]) - num]["quantity"]
                flagSell = 1
            else:
                num += 1
        else:
            num += 1
    else:
        num += 1

>Solution :

Try to use bult-in function min() to find minimum order according to the platinum key (data is your dictionary from the question):

min_order = min(data["payload"]["orders"], key=lambda o: o["platinum"])

print("Min Platinum =", min_order["platinum"])
print("Name =", min_order["user"]["ingame_name"])
print("Quantity =", min_order["quantity"])

Prints:

Min Platinum = 4
Name = Noxxat
Quantity = 1

EDIT: If you want to search for a minimum in orders where order_type == 'sell':

min_order = min(
    (o for o in data["payload"]["orders"] if o["order_type"] == "sell"),
    key=lambda o: o["platinum"],
)

print("Min Platinum =", min_order["platinum"])
print("Name =", min_order["user"]["ingame_name"])
print("Quantity =", min_order["quantity"])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading