0

I have JSON data looks like this:

data2 = [{'name': '', 'steps': [{'number': 1, 'step': 'Place olive oil on the bottom of your Instant Pot before adding chicken breasts to the Instant Pot.', 'ingredients': [{'id': 4053, 'name': 'olive oil', 'image': 'olive-oil.jpg'}], 'equipment': [{'id': 414093, 'name': 'instant pot', 'image': ''}]}, {'number': 2, 'step': 'Season chicken breast with salt and pepper.', 'ingredients': [{'id': 1102047, 'name': 'salt and pepper', 'image': 'salt-and-pepper.jpg'}], 'equipment': []}, {'number': 3, 'step': 'Close Instant Pot and make sure the pressure gauge is turned to seal. Set pressure to manual, high pressure and set the timer for 8 minutes. Pressure will build and then the timer will start.', 'ingredients': [], 'equipment': [{'id': 414093, 'name': 'instant pot', 'image': ''}, {'id': 404695, 'name': 'kitchen timer', 'image': 'kitchen-timer.jpg'}], 'length': {'number': 8, 'unit': 'minutes'}}, {'number': 4, 'step': 'When the timer goes off, quick release the Instant Pot by turning pressure gauge to vent. When pressure releases shred chicken breast in the Instant Pot and serve any way you like.', 'ingredients': [], 'equipment': [{'id': 414093, 'name': 'instant pot', 'image': ''}, {'id': 404695, 'name': 'kitchen timer', 'image': 'kitchen-timer.jpg'}]}]}]

And I want to get the number and step, so that my output print looks like this:

1 Place olive oil on the bottom of your Instant Pot before adding chicken breasts to the Instant Pot.
2 Season chicken breast with salt and pepper.
3 Close Instant Pot and make sure the pressure gauge is turned to seal. Set pressure to manual, high pressure and set the timer for 8 minutes. Pressure will build and then the timer will start.
4 When the timer goes off, quick release the Instant Pot by turning pressure gauge to vent. When pressure releases shred chicken breast in the Instant Pot and serve any way you like.

My current code looks like:

for item in data2[0]['steps']:
    print(item['step'])

And this only prints out all the steps but not the number. How can I print out the value of "number" also?

5
  • try item["number"] Commented Apr 23, 2020 at 1:21
  • you can do this: print(f"{item['number']} {item['step']}") Commented Apr 23, 2020 at 1:23
  • Or print(item['number'], item['step']) Commented Apr 23, 2020 at 1:24
  • But I had to use for loop for item in data2[0]['step'] to get access to each item... Commented Apr 23, 2020 at 1:24
  • No problem, the code above just works. Commented Apr 23, 2020 at 1:26

1 Answer 1

1

you can try following code:

In [22]: steps = data2[0].get('steps',None)

In [23]: steps
Out[23]:
[{'number': 1,
  'step': 'Place olive oil on the bottom of your Instant Pot before adding chicken breasts to the Instant Pot.',
  'ingredients': [{'id': 4053, 'name': 'olive oil', 'image': 'olive-oil.jpg'}],
  'equipment': [{'id': 414093, 'name': 'instant pot', 'image': ''}]},
 {'number': 2,
  'step': 'Season chicken breast with salt and pepper.',
  'ingredients': [{'id': 1102047,
    'name': 'salt and pepper',
    'image': 'salt-and-pepper.jpg'}],
  'equipment': []},
 {'number': 3,
  'step': 'Close Instant Pot and make sure the pressure gauge is turned to seal. Set pressure to manual, high pressure and set the timer for 8 minutes. Pressure will build and then the timer will start.',
  'ingredients': [],
  'equipment': [{'id': 414093, 'name': 'instant pot', 'image': ''},
   {'id': 404695, 'name': 'kitchen timer', 'image': 'kitchen-timer.jpg'}],
  'length': {'number': 8, 'unit': 'minutes'}},
 {'number': 4,
  'step': 'When the timer goes off, quick release the Instant Pot by turning pressure gauge to vent. When pressure releases shred chicken breast in the Instant Pot and serve any way you like.',
  'ingredients': [],
  'equipment': [{'id': 414093, 'name': 'instant pot', 'image': ''},
   {'id': 404695, 'name': 'kitchen timer', 'image': 'kitchen-timer.jpg'}]}]

In [24]: for step in steps:
    ...:     print(step['number'],' ',step['step'])
    ...:
1   Place olive oil on the bottom of your Instant Pot before adding chicken breasts to the Instant Pot.
2   Season chicken breast with salt and pepper.
3   Close Instant Pot and make sure the pressure gauge is turned to seal. Set pressure to manual, high pressure and set the timer for 8 minutes. Pressure will build and then the timer will start.
4   When the timer goes off, quick release the Instant Pot by turning pressure gauge to vent. When pressure releases shred chicken breast in the Instant Pot and serve any way you like.
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, it works like a magic. Thank you! I never knew about the .get function on a dictionary. I'm trying on other data... Thank you so much
I tried to do it right away but it told me to wait a few min.. but i wont forget!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.