Categories
Mastering Development

Get values in all strings of JSON

{
  "cars": {
    "Nissan": {
      "detail": {"doors":4, "transmission":"automatic"}
    },
    "Honda": {
      "detail": {"doors":4, "transmission":"automatic"}
    },
    "Ford": {
      "detail": {"doors":4, "transmission":"automatic"}
    }
  }
}

How can I make some looping to get the details of all manufacturers? I’m currently using python. I tried to do it, but I will need to write the manufacturer. If it isn’t possible to create a looping for it, can I access the details of cars without directly inform the Manufacturer?

What I tried:

with open("data/testedsds.json") as data_file:
  data = json.load(data_file)
  for manufacturer in data["cars"][0][""]:
    detail = manufacturer["detail"]

This code works great, but I would need to complete the empty [" "] with some of the manufacturers inside. like ["Honda"]

Edit:
From Ayush answer I can get the values, but I need to get them individually

I tried this:

for manufacturer, details in data["cars"].items():
  #print(manufacturer, details)
  for details, doors in details.items(): 
     #print(doors)
     for doors, doors2 in doors.items():
       print(doors2)

Output:

4
automatic
4
automatic
4
automatic

This code get the port value, but get together the transmission mode. Is it possible to get one the doors number?

Leave a Reply

Your email address will not be published. Required fields are marked *