Search code examples
amazon-web-servicespython-requestsaws-secrets-manager

I want get value for specific key(sp_puser,sp_pkey) from below dictionary object in python can anyone suggest


{
    'ARN': 'arn:aws:secretsmanager:us-west-1:704532713440543:secret:mysecret-XUXMVY',
    'Name': 'mysecret',
    'VersionId': 'h5757v76-6666-9977-0099-7jhhhvnb987967',
    'SecretString': '{"sp_puser":"ALKJLK897MNVMJH2YU","sp_pkey":"dsjjhGHkjhjg7GJchjchkh"}',
    'VersionStages': ['AWSCURRENT'],
    'CreatedDate': datetime.datetime(2021, 6, 17, 15, 7, 48, 971000, tzinfo=tzlocal()),
    'ResponseMetadata': {
        'RequestId': 'h5757v76-6666-9977-0099-7jhhhvnb987967',
        'HTTPStatusCode': 200,
        'HTTPHeaders': {
            'date': 'Thu, 24 Jun 2021 12:59:41 GMT',
            'content-type': 'application/x-amz-json-1.1',
            'content-length': '343',
            'connection': 'keep-alive',
            'x-amzn-requestid': 'h5757v76-6666-9977-0099-7jhhhvnb987967'
        },
        'RetryAttempts': 0
    }
}

Solution

  • This is a JSON-string, use json.loads() to parse it.

    data = {
        # ...
        'SecretString': '{"sp_puser":"ALKJLK897MNVMJH2YU","sp_pkey":"dsjjhGHkjhjg7GJchjchkh"}',
        # ...
    }
    
    import json
    secret_string = json.loads(data["SecretString"])
    print(secret_string["sp_puser"]) # ALKJLK897MNVMJH2YU
    print(secret_string["sp_pkey"]) # dsjjhGHkjhjg7GJchjchkh