Home > Software design >  Retrieve a value based on a key in json array using python
Retrieve a value based on a key in json array using python

Time:07-22

My json data looks something like this:

{
  "kind": "admin#reports#usageReport",
  "date": "2022-07-17",
  "etag": "\"dng2uCItaXPqmMj2MG4RUqVkRjnE_4kf0VvQ0_WkiTg/dICFK4HeNet7m-jGyh4UpD7jLI4\"",
  "entity": {
    "type": "USER",
    "customerId": "H01234sux",
    "userEmail": "[email protected]",
    "profileId": "123456789999012345678"
  },
  "parameters": [
    {
      "name": "gmail:last_access_time",
      "datetimeValue": "2022-07-08T00:06:16.000Z"
    },
    {
      "name": "gmail:num_emails_exchanged",
      "intValue": "0"
    },
    {
      "name": "accounts:first_name",
      "stringValue": "Harry"
    },
    {
      "name": "accounts:last_name",
      "stringValue": "Potter"
    },
    {
      "name": "accounts:is_disabled",
      "boolValue": false
    },
    {
      "name": "accounts:disabled_reason"
    },
    {
      "name": "accounts:creation_time",
      "datetimeValue": "2022-05-26T21:54:18.000Z"
    },
    {
      "name": "accounts:last_login_time",
      "datetimeValue": "1970-01-01T00:00:00.000Z"
    },
    {
      "name": "accounts:is_super_admin",
      "boolValue": false
    },
    {
      "name": "accounts:is_delegated_admin",
      "boolValue": false
    },
    {
      "name": "accounts:drive_used_quota_in_mb",
      "intValue": "0"
    }
  ]
}

I am trying to retrieve the "Values" of a certain "names" only and came up with below code

        for user in users:
        uemail = user['entity']['userEmail'];
        for param in user['parameters']:
            if param['name'] == 'gmail:last_webmail_time':
                lwmtm = param['datetimeValue'];
            if param['name'] == 'accounts:total_quota_in_mb':
                ttlqmb = param['intValue']
            if param['name'] == 'accounts:used_quota_in_mb':
                usqmb = param['intValue']
        print(u'{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}'.format(uemail,lwmtm,lintm,lactm,acdstr,accrtm,aclltm,aclstm,drusmb,gmusmb,gpusmb,ttlqmb,usqmb))

i didn't like the multiple "IF" statements and wondering if there any optimized way in python to only pick "*Values" of the required "name" fields, please suggest.

CodePudding user response:

since Python 3.10 (latest) we have match like other languages. I modified your code a bit to show it:

users = {
    "kind": "admin#reports#usageReport",
    "date": "2022-07-17",
    "etag": "\"dng2uCItaXPqmMj2MG4RUqVkRjnE_4kf0VvQ0_WkiTg/dICFK4HeNet7m-jGyh4UpD7jLI4\"",
    "entity": {
        "type": "USER",
        "customerId": "H01234sux",
        "userEmail": "[email protected]",
        "profileId": "123456789999012345678"
    },
    "parameters": [
        {
            "name": "gmail:last_access_time",
            "datetimeValue": "2022-07-08T00:06:16.000Z"
        },
        {
            "name": "gmail:num_emails_exchanged",
            "intValue": "0"
        },
        {
            "name": "accounts:first_name",
            "stringValue": "Harry"
        },
        {
            "name": "accounts:last_name",
            "stringValue": "Potter"
        },
        {
            "name": "accounts:is_disabled",
            "boolValue": False
        },
        {
            "name": "accounts:disabled_reason"
        },
        {
            "name": "accounts:creation_time",
            "datetimeValue": "2022-05-26T21:54:18.000Z"
        },
        {
            "name": "accounts:last_login_time",
            "datetimeValue": "1970-01-01T00:00:00.000Z"
        },
        {
            "name": "accounts:is_super_admin",
            "boolValue": False
        },
        {
            "name": "accounts:is_delegated_admin",
            "boolValue": False
        },
        {
            "name": "accounts:drive_used_quota_in_mb",
            "intValue": "0"
        }
    ]
}


user_email = users['entity']['userEmail']
for param in users['parameters']:
    #print(param['name'])
    match param['name']:
        case "gmail:last_access_time":
            lwmtm = param['datetimeValue']
        case 'accounts:first_name':
            ttlqmb = param['stringValue']
        case 'accounts:last_name':
            usqmb = param['stringValue']
            
print(f"{lwmtm} - {ttlqmb} - {usqmb}")
  • Related