How do you enumerate a JSON return? The goal is to get a total count variable to add to my print statment...
I have
for i in services_js["reports"]:
print " Service Name: " + i["instances"]["serviceName"]
and have tried:
for i in enumerate(services_js["reports"]):
print " Service Name: " + i["instances"]["serviceName"]
but get:
TypeError: tuple indices must be integers, not str
This is NOT how enumerate
works:
for i in enumerate(services_js["reports"]):
print " Service Name: " + i["instances"]["serviceName"]
It works like this:
animals = ['rat', 'cat', 'mouse', 'dog']
for i, animal in enumerate(animals, start=1):
print('# {}: {}'.format(i, animal))
Notice how enumerate returns a tuple, (i
and animal
) not just the count?
You may have other issues (in your JSON) but that part might be throwing you off.
The error you are seeing is because i
is actually the tuple (0, some_json)
and then you're trying to do i['instance']
, so it's saying you can't use a string as an index to a tuple.