0

I am getting below sample data through get api call in json format. trying to parse the 'members' data form json but getting KeyError.

Sample data:

{
  "data": {
    "searchResult": [
      {
        "teamName": "abc comp",
        "startDate": "20190901",
        "status": "Open",
        "category": "abc Service",
        "domain": "abc comp",
        "teamSFId": "a5nf00000000OPqAAM",
        "asOfDate": "20190927",
        "runDateTime": "20190927T210031+0000"
      },
      {
        "teamName": "abc Reporting",
        "startDate": "20181129",
        "status": "Open",
        "category": "abc Service",
        "domain": "abc comp",
        "teamSFId": "a5n0R0000002hqLQAQ",
        "asOfDate": "20190919",
        "runDateTime": "20190919T184824+0000"
      },
      {
        "teamName": "abc 766",
        "startDate": "20190702",
        "status": "Open",
        "category": "abc Service",
        "domain": "abc comp",
        "teamSFId": "a5n0R0000003InpQAE",
        "asOfDate": "20190919",
        "runDateTime": "20190919T184824+0000"
      },
      {
        "teamName": "abc comp 1",
        "closeDate": "20190127",
        "startDate": "20190125",
        "status": "Closed",
        "category": "abc Service",
        "domain": "abc comp",
        "teamSFId": "a5n0R000000DNoAQAW",
        "members": [
          {
            "memberName": "TM0000005872",
            "memberSFId": "a5m0R000000DDIYQA4",
            "active": false,
            "contactName": "intcontact 1",
            "coreTeamMember": "Yes",
            "endDate": "20190127",
            "personId": 0,
            "hybridRoleFlag": false,
            "launchMember": false,
            "role": "abc Analyst",
            "startDate": "20190125",
            "teamLeader": "No"
          }
        ],
        "asOfDate": "20190919",
        "runDateTime": "20190919T184824+0000"
      },
      {
        "teamName": "abc comp D",
        "startDate": "20191022",
        "status": "Open",
        "teamCode": "TC100004",
        "teamSFId": "a5nf00000004U5gAAE",
        "asOfDate": "20191104",
        "runDateTime": "20191104T163644+0000"
      },
      {
        "teamName": "abc comp 766",
        "startDate": "20190703",
        "status": "Open",
        "category": "abc Service",
        "domain": "abc comp",
        "teamSFId": "a5n0R0000003JRpQAM",
        "members": [
          {
            "memberName": "TM0000006456",
            "memberSFId": "a5m0R0000000UYvQAM",
            "active": true,
            "contactName": "Leonel",
            "coreTeamMember": "Yes",
            "personId": 31429,
            "hybridRoleFlag": false,
            "launchMember": false,
            "primaryTeamAssociation": "No",
            "role": "TransporManager",
            "startDate": "20190703",
            "teamLeader": "No",
            "samAccountName": "LeoM"
          }
        ],
        "asOfDate": "20190919",
        "runDateTime": "20190919T184824+0000"
      },
      {
        "teamName": "Test abc Service",
        "startDate": "20191119",
        "distributionList": "Angela Li",
        "status": "Open",
        "teamCode": "TC100014",
        "teamDesc": "testing",
        "teamSFId": "a5nf00000004UZvAAM",
        "asOfDate": "20191119",
        "runDateTime": "20191119T100022+0000"
      },
      {
        "teamName": "MLB abc comp Team 1",
        "startDate": "20181101",
        "status": "Open",
        "category": "abc Service",
        "domain": "abc comp",
        "teamSFId": "a5n0R0000004TC3QAM",
        "asOfDate": "20190919",
        "runDateTime": "20190919T184824+0000"
      }
    ]
  },
  "status": "success",
  "code": "0",
  "errorMsg": null,
  "httpResponseCode": 200
}

Code:

import requests
import json

targeturl = 'https://xyz.abc.com' 
response = requests.get(targeturl, auth=('username', 'password'))
content = response.content.decode('utf-8') 
json_data = json.loads(content) 

team = json_data['data']['searchResult']
for team_data in team:
    print(team_data) #Able to parse team data
member = team_data['members'] # -> getting error while parsing member data

quote Error: KeyError Traceback (most recent call last) in ----> 1 member = team_data['members']

KeyError: 'members'

quote

4
  • Not every object inside searchResults (which your code bound to team_data) has the members key - try using team_data.get('members') instead to avoid the KeyError, but at the expense of having None be bound to members. Commented Jan 24, 2020 at 6:16
  • Perhaps there is no Key 'members' in team_data. Commented Jan 24, 2020 at 6:16
  • yeah some team may not have members associated with , i need to extract member data only for the team it exist Commented Jan 24, 2020 at 6:30
  • could you please help , still facing the issue Commented Jan 27, 2020 at 15:56

1 Answer 1

1

You have to check if member field id present in the given team_data variable. It can be done by using

if "members" in team_data.keys()
    member=team_data["members"]

As there are members in some team_data but not present in some

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the insight. team_data.keys() gives - > dict_keys(['teamName', 'startDate', 'status', 'category', 'domain', 'teamSFId', 'asOfDate', 'runDateTime']) . Do i need to use any loop for this check?
could you please help , still facing the issue.
In some of team_data there is no 'members' field so you can access it. Due to this above condition check for members key is team_data variable using "members" in team_data.keys() as it return true then only it can parse the given team_data members field

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.