Sumanth Shetty
Sumanth Shetty

Reputation: 677

How to iterate through a json object

My question is in 2 parts .

I have a json file :

"result": [
             {
               "id": "12345678",
               "label": "Target Label",
               "hostName": "www.example.com",
               "location": "Seattle, WA",
               "locationid": "9",
               "targetType": "PING basic",
               "frequency": "5",
               "statusCode": "UP",
               "statusMessage": "OK",
               "fails": "0",
               "responseTime": "0.136",
               "dnsTime": "0.000",
               "connectTime": "0.136",
               "redirectTime": "0.000",
               "firstbyteTime": "0.000",
               "lastbyteTime": "0.000",
               "lastCheck": "15:43 12-05-2017",
               "lastDown": "21:34 11-17-2017",
               "pingLoss": "0.00",
               "pingMin": "136.357",
               "pingAvg": "136.597",
               "pingMax": "137.049"
             }
           ]

Question 1: Assuming the json files contain N number of "result".

How can I access the json results in a loop ?

I am doing this to access a from single result :

data = {}
data['PublisherMessage'] = data_dict['result'][0]['statusMessage']

How can I access the json results in a loop with multiple results?

Question 2:

How can I create dictionary in loop to assign it to json results which I will be fetching in loop?

Upvotes: 0

Views: 65

Answers (1)

quamrana
quamrana

Reputation: 39354

Assuming you have data_dict whose only entry is data_dict['result'] then this is a list which you can iterate over:

for item in data_dict['result']:
    # do something with item['statusMessage'] etc

Upvotes: 1

Related Questions