0

I am having a json data like this and I want the specific data from this json format based on the condition given below

{
    "ok": true,
    "members": [
        {
            "id": "W012A3CDE",
            "team_id": "T012AB3C4",
            "name": "spengler",
            "deleted": false,
            "color": "9f69e7",
            "real_name": "spengler",
            "tz": "America/Los_Angeles",
            "tz_label": "Pacific Daylight Time",
            "tz_offset": -25200,
            "profile": {
                "avatar_hash": "ge3b51ca72de",
                "status_text": "Print is dead",
                "status_emoji": ":books:",

            }

        },
        {
            "id": "W07QCRPA4",
            "team_id": "T0G9PQBBK",
            "name": "glinda",
            "deleted": false,
            "color": "9f69e7",
            "real_name": "Glinda Southgood",
            "tz": "America/Los_Angeles",
            "tz_label": "Pacific Daylight Time",
            "tz_offset": -25200,
            "profile": {
                "phone": "",
                "skype": "",
                "real_name": "Glinda Southgood",
                "real_name_normalized": "Glinda Southgood",
                "display_name": "Glinda the Fairly Good",
                "display_name_normalized": "Glinda the Fairly Good",
                "email": "[email protected]"
            },

        }
    ],
    "cache_ts": 1498777272,
    "response_metadata": {
        "next_cursor": "dXNlcjpVMEc5V0ZYTlo="
    }
}

now I want to get only name and id from the members where deleted:false from this json format using python Can anyone help me

1

1 Answer 1

1

Just a simple list comprehension should be enough assuming you've managed to load the json into a dict.

response = json.loads(<your_json_string_here>)
undeleted_members = [dict(id=member['id'], name=member['name']) for member in response['members'] if not member['deleted']]
print(undeleted_members)

Returns a list of dicts with just the name & ID like:

{'id': 'W07QCRPA4', 'name': 'glinda'}

Or if you want separate lists for IDs & names:

all_ids = [member['id'] for member in response['members'] if not member['deleted']]
all_names = [member['name'] for member in response['members'] if not member['deleted']]
Sign up to request clarification or add additional context in comments.

7 Comments

with your code I am getting error like this TypeError: the JSON object must be str, bytes or bytearray, not Response can you help me @ rdas
You have a response object. You need to get the response data (probably from response.text) before passing it to json.loads()
How to get the response.text can you tell me
Yes got it but I want to get it as an list of arrays is it possible
2 separate lists for id & name or a list of tuples like [(id, name), (id, name), ...`
|

Your Answer

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