So I have been playing around with JSONand been trying to print out but without any luck. Basically what JSONI am trying to print out is the cityplace as you will see here:
{
'id': '5235246c-ac21a7-151128-8cd9-512512',
'type': 'Hello world',
'metadata': {
'invite_text': "A very cool text here!!",
'cityplace': [
{
'display_text': 'Stackoverflow City',
}
]
}
}
What I have done for code is that I made a loop that prints out by calling the name test_print which gives me the JSON object that I printed out.
What I did try was
print(test_print['metadata']['cityplace']['display_text'])
unfortunately this give me error
TypeError: list indices must be integers or slices, not str
So what I needed to do is:
print(test_print['metadata']['cityplace'][0]['display_text'])
and now my question is: Is it possible to print out without needing to add [0] because it might not always be 0 in the future?
print(test_print['metadata']['cityplace'][0]['display_text'])?[0], if you want to print every item you can iterate through with a loopfor item in test_print['metadata']['cityplace']: print(item['display_text']).