python iterate json code example
Example 1: for each python json
import json
def main():
jsonString = '{"key1":"value1","key2":"value2","key3":"value3"}'
jsonObject = json.loads(jsonString)
for key in jsonObject:
value = jsonObject[key]
print("The key and value are ({}) = ({})".format(key, value))
pass
if __name__ == '__main__':
main()
Example 2: python iterate json file
import json
with open('items.json') as data_file:
data = json.load(data_file)
Example 3: loop through json array python
for restaurant in data['restaurants']:
print restaurant['restaurant']['name']