-1

I have a nested json file with the below data and I need some help in writing an interactive program where it should take a topping as input and prints out the names of all the donuts that have this topping.

[
    {
        "id": "0001",
        "type": "donut",
        "name": "Cake",
        "ppu": 0.55,
        "batters":
            {
                "batter":
                    [
                        { "id": "1001", "type": "Regular" },
                        { "id": "1002", "type": "Chocolate" },
                        { "id": "1003", "type": "Blueberry" },
                        { "id": "1004", "type": "Devil's Food" }
                    ]
            },
        "topping":
            [
                { "id": "5001", "type": "None" },
                { "id": "5002", "type": "Glazed" },
                { "id": "5005", "type": "Sugar" },
                { "id": "5007", "type": "Powdered Sugar" },
                { "id": "5006", "type": "Chocolate with Sprinkles" },
                { "id": "5003", "type": "Chocolate" },
                { "id": "5004", "type": "Maple" }
            ]
    },
    {
        "id": "0002",
        "type": "donut",
        "name": "Raised",
        "ppu": 0.55,
        "batters":
            {
                "batter":
                    [
                        { "id": "1001", "type": "Regular" }
                    ]
            },
        "topping":
            [
                { "id": "5001", "type": "None" },
                { "id": "5002", "type": "Glazed" },
                { "id": "5005", "type": "Sugar" },
                { "id": "5003", "type": "Chocolate" },
                { "id": "5004", "type": "Maple" }
            ]
    }
]

What I've tried so far is

import json
f = open('Nested-JSON-1.json')
data = json.load(f)
print(data)

The above code gave the output with all the data from the nested json file

[{'id': '0001', 'type': 'donut', 'name': 'Cake', 'ppu': 0.55, 'batters': {'batter': [{'id': '1001', 'type': 'Regular'}, {'id': '1002', 'type': 'Chocolate'}, {'id': '1003', 'type': 'Blueberry'}, {'id': '1004', 'type': "Devil's Food"}]}, 'topping': [{'id': '5001', 'type': 'None'}, {'id': '5002', 'type': 'Glazed'}, {'id': '5005', 'type': 'Sugar'}, {'id': '5007', 'type': 'Powdered Sugar'}, {'id': '5006', 'type': 'Chocolate with Sprinkles'}, {'id': '5003', 'type': 'Chocolate'}, {'id': '5004', 'type': 'Maple'}]}, {'id': '0002', 'type': 'donut', 'name': 'Raised', 'ppu': 0.55, 'batters': {'batter': [{'id': '1001', 'type': 'Regular'}]}, 'topping': [{'id': '5001', 'type': 'None'}, {'id': '5002', 'type': 'Glazed'}, {'id': '5005', 'type': 'Sugar'}, {'id': '5003', 'type': 'Chocolate'}, {'id': '5004', 'type': 'Maple'}]}, {'id': '0003', 'type': 'donut', 'name': 'Old Fashioned', 'ppu': 0.55, 'batters': {'batter': [{'id': '1001', 'type': 'Regular'}, {'id': '1002', 'type': 'Chocolate'}]}, 'topping': [{'id': '5001', 'type': 'None'}, {'id': '5002', 'type': 'Glazed'}, {'id': '5003', 'type': 'Chocolate'}, {'id': '5004', 'type': 'Maple'}]}]

And then I added below which gave me the error

for type in data['type']:
    for topping in type['topping']:
        print(topping.get('name'))

json.dumps(data)

TypeError: list indices must be integers or slices, not str

bad_coder
  • 11,289
  • 20
  • 44
  • 72
user7463647
  • 53
  • 1
  • 8
  • The root structure is an array. There is no `type` property. Also objects inside `topping` have no `name` property. – gre_gor Jan 25 '22 at 06:19

1 Answers1

1

Try:

import json

topping = input('Enter topping: ')

names = []
with open('data.json') as fp:
    data = json.load(fp)
    for rec in data:
        if rec['type'] == 'donut':
            for top in rec['topping']:
                if top['type'] == topping:
                    names.append(rec['name'])

if len(names) > 0:
    print('Found matches:')
    print(*[f'- {name}' for name in names], sep='\n')
else:
    print('No matches found')

Output:

Enter topping: Sugar
Found matches:
- Cake
- Raised

Enter topping: Cinnamon
No matches found
Corralien
  • 109,409
  • 8
  • 28
  • 52
  • Got it. I have added else: Print('No Matches Found'), when I am trying to give a topping which is not in the json data, it is giving me the output as multiple No Matches Found. Enter topping: Cinnamon No Matches Found No Matches Found No Matches Found No Matches Found No Matches Found No Matches Found No Matches Found No Matches Found – user7463647 Jan 25 '22 at 06:41
  • @user7463647. I updated my answer. Can you check it please? – Corralien Jan 28 '22 at 11:10