1

I've got a JSON structure that looks like the following

{
  "PersonInformation": {
    "PhysicalStatus": "",
    "OpenDetainers": [],
    "StartDate": "",
    "FacilityLog": [],
    "CustStatus": "",
    "EndDate": ""
  },
  "IdentityList": [
    {
      "CreationDate": "01/01/1999",
      "PersonNames": [
        {
          "Suffix": "",
          "FirstName": "Johnny",
          "LastName": "Appleseed",
          "MiddleName": ""
        },
        {
          "Suffix": "",
          "FirstName": "Foo",
          "LastName": "Bar",
          "MiddleName": ""
        }
      ],
      "PlaceOfBirthList": [
        {
          "City": "Boston",
          "State": "MA",
          "CountryCode": ""
        }
      ]
    }
  ]
}

I can parse the outer array like so, but I'm having trouble figuring out how to loop through one of the child arrays, like "PersonNames"

So I can do this

myjson = json.loads(json_data)
print myjson['PersonInformation']['PhysicalStatus']
for identity_list in myjson['IdentityList']:
    print identity_list['CreationDate']

Which returns

OK
01/01/1999 

as expected, but I don't know how to take it to the next level to traverse into and loop through "PersonNames"

Thanks for the assistance

0

1 Answer 1

2

You can iterate through the sub-list under the PersonNames key like this:

for identity in myjson['IdentityList']:
    for person in identity['PersonNames']:
        print person['FirstName'], person['LastName']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.