How do we get dict value from object with changing variable

Viewed 51
x = json.loads(y) #y is received from sender

print(x)

Output:

[
    [
        {
            "O2JUKZ-4QUHH-6ZUZWF": {
                "avg_price": "0.00000",
                "cost": "0.00000",
                "descr": {
                    "close": None,
                    "leverage": "0"
                },
                "status": "open",
                "stoprice": "0.00000"
            }
        }
    ],
    "openOrders",
    {
        "sequence": 1
    }
]

Question: How do we assign order number (the characters in bold) to a variable, when It always changes?

Already tried: x[0][0]['O2JUKZ-4QUHH-6ZUZWF']['status'] to get the status (which works), but doesn't work anymore when "O2JUKZ-4QUHH-6ZUZWF" changes.

How do we assign order number to variable?

1 Answers

You could iterate over the dictionary with .items()

my_dictionary = data[0][0]
for order_code, sub_dict in my_dictionary.items():
    # order_code is the dictionary key and order code you want
    # sub_dict.get("status") will be the status
Related