Using JSON() method to get a python object

Screen Link:

My Code:

I am curious about what the [0] does here. Is it an argument that json() uses? If so, what is it referring to? Is it an index number? If not, what would changing this number do? Very confusing.

first_pass_duration = json_data["response"][0]["duration"]

It’s excellent that you are curious about this!

So, here is the answer you actually need to satisfy your curiosity -

Print and play around with the code

  • You have json_data, print it. See what it outputs.
  • You have json_data["response"], print it. See what it outputs.
  • You have json_data["response"][0], print it. See what it outputs.
  • You have json_data["response"][0], change that 0 to something else and print it. See what it outputs.

Based on all the above steps, make a conclusion about what is happening and what happens when you change things. That’s what will resolve your confusion AND help you resolve future confusions about similar questions.

Print things out and play with your code! Feel free to ask further questions if need be.

2 Likes

Will do! This is very helpful for not just this problem, but others! Thank you.

2 Likes