Getting a value of every item in a json file

Viewed 19

So i want to get all of the prc values from this json file and add them together. I was trying for awhile but none of my ideas worked out

file.json

{
    "prcprice": 10,
    "983712735957229578": {
        "wallet": 693896,
        "prc": 70006
    },
    "453209412102782986": {
        "wallet": 69410,
        "prc": 301
    },
    "475293506295037952": {
        "wallet": 9999,
        "prc": 1000
    }
}
1 Answers

Didn't understand if you want to calculate "prcprice" if not just paste "pass" in except statement instead of "value = data[key]". Note that I named json dict as "data".

values = []
for key in data:
    try:
        value = data[key].get('prc')
    except AttributeError:
        value = data[key]
    values.append(value)
res = sum(values)
print(values)
print(res)
Related