iterating through a json object/dictionary

579
December 21, 2016, at 02:01 AM
abc = {
"orders": [
    {
        "orderID": 5,
        "cost": 10,
        "sell": 15
    },
    {
        "orderID": 6,
        "cost": 8,
        "sell": 12
    },
    {
        "orderID": 7,
        "cost": 15,
        "sell": 26           
        }
    ]
}
for key, value in abc.items():
print (value["orderID"])

I am trying to extract the orderID values, but cant seem to make this work. It should respond with 5,6,7

Answer 1

right now you're looping over the abc dict. Instead, you want to loop over abc['orders']:

for order in abc['orders']:
    print (order["orderID"])
Answer 2
for d in abc['orders']:
    print (d["orderID"])

abc has a key orders which is a list.So you can iterate over the list.

Answer 3

The first think you need to do if you aren't done it already is to parse your file to a python dictionary. If you don't know how here is Link.

The next thing to do is to iterate through a nested dictionary which you can see how to do here: Link.

After doing those two steps you would be able to get all the keys and values you need in order to make further calculations.

Hope I helped you. I am new to writing answers not asking the questions. :)

Rent Charter Buses Company
READ ALSO
python: logging set upper severity level

python: logging set upper severity level

I'm trying to partition DEBUG, INFO, and ERROR into seperate files due to a requirement

568
Why does one software slow down when receiving data from another via UDP (python)?

Why does one software slow down when receiving data from another via UDP (python)?

I'm constantly sending a camera's position matrix via UDP from Houdini and setting it in Cinema4dBoth are 3-D software programs

448
KafkaConsumer class usuage?

KafkaConsumer class usuage?

I am learning KafkaConsumer and have a question in line 3

381
Python Dictionary Code just loops over and over again

Python Dictionary Code just loops over and over again

I have a text file I am designing for a mock store I named 'Bodn's Superstore'I designed a

329