11

I am trying to parse a JSON data set that looks something like this:

{"data":[
 {
 "Rest":0,
 "Status":"The campaign is moved to the archive",
 "IsActive":"No",
 "StatusArchive":"Yes",
 "Login":"some_login",
 "ContextStrategyName":"Default",
 "CampaignID":1111111,
 "StatusShow":"No",
 "StartDate":"2013-01-20",
 "Sum":0,
 "StatusModerate":"Yes",
 "Clicks":0,
 "Shows":0,
 "ManagerName":"XYZ",
 "StatusActivating":"Yes",
 "StrategyName":"HighestPosition",
 "SumAvailableForTransfer":0,
 "AgencyName":null,
 "Name":"Campaign_01"
 },
 {
 "Rest":82.6200000000008,
 "Status":"Impressions will begin tomorrow at 10:00",
 "IsActive":"Yes",
 "StatusArchive":"No",
 "Login":"some_login",
 "ContextStrategyName":"Default",
 "CampaignID":2222222,
 "StatusShow":"Yes",
 "StartDate":"2013-01-28",
 "Sum":15998,"StatusModerate":"Yes",
 "Clicks":7571,
 "Shows":5535646,
 "ManagerName":"XYZ",
 "StatusActivating":"Yes",
 "StrategyName":"HighestPosition",
 "SumAvailableForTransfer":0,
 "AgencyName":null,
 "Name":"Campaign_02"
 }
 ]
}

Lets assume that there can be many of these data sets.

I would like to iterate through each one of them and grab the "Name" and the "Campaign ID" parameter.

So far my code looks something like this:

decoded_response = response.read().decode("UTF-8")
data = json.loads(decoded.response)
 for item in data[0]:
 for x in data[0][item] ...
 -> need a get name procedure
 -> need a get campaign_id procedure

Probably quite straight forward! I am not good with lists/dictionaries :(

gsamaras
73.7k50 gold badges210 silver badges330 bronze badges
asked May 21, 2013 at 17:18
1
  • 2
    What good is python without lists and dictionaries? Commented May 21, 2013 at 17:37

1 Answer 1

22

Access dictionaries with d[dict_key] or d.get(dict_key, default) (to provide default value):

jsonResponse=json.loads(decoded_response)
jsonData = jsonResponse["data"]
for item in jsonData:
 name = item.get("Name")
 campaignID = item.get("CampaignID")

I suggest you read something about dictionaries.

gsamaras
73.7k50 gold badges210 silver badges330 bronze badges
answered May 21, 2013 at 18:40
Sign up to request clarification or add additional context in comments.

1 Comment

It worked! Hooray! Also read all about dictionaries. Thank you :-)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.