简体   繁体   中英

Python 3.8 - Unable to update dictionary

Hello I have this below dictionary that I want to update

{
   "Version":"2012-10-17",
   "Statement":[
      {
         "Effect":"Allow",
         "Principal":{
            "Federated":"arn:aws:iam::111111111111:saml-provider/Test"
         },
         "Action":"sts:AssumeRoleWithSAML",
         "Condition":{
            "StringEquals":{
               "SAML:aud":"https://signin.aws.amazon.com/saml"
            }
         }
      }
   ]
}

Want to update it to

{
   "Version":"2012-10-17",
   "Statement":[
      {
         "Effect":"Allow",
         "Principal":{
            "Federated":[
            "arn:aws:iam::111111111111:saml-provider/Test", 
            "arn:aws:iam::111111111111:saml-provider/Test2"
            ]
         },
         "Action":"sts:AssumeRoleWithSAML",
         "Condition":{
            "StringEquals":{
               "SAML:aud":"https://signin.aws.amazon.com/saml"
            }
         }
      }
   ]
}

ie add "arn:aws:iam::111111111111:saml-provider/Test2" to "Federated" and also make it a list. Below is my code

    new_arn = "arn:aws:iam::111111111111:saml-provider/Test2"
    my_dict = {
   "Version":"2012-10-17",
   "Statement":[
      {
             "Effect":"Allow",
             "Principal":{
                "Federated":[
                "arn:aws:iam::111111111111:saml-provider/Test",
                ]
             },
             "Action":"sts:AssumeRoleWithSAML",
             "Condition":{
                "StringEquals":{
                   "SAML:aud":"https://signin.aws.amazon.com/saml"
                }
             }
          }
       ]
    }
    for b in my_dict['Statement']:
        updated_arn = f"['{b['Principal']['Federated']}', {new_arn}]"
        b['Principal']['Federated']: updated_arn

    print(my_dict)

I am bit new to python and I am not sure what am I doing wrong the dict is not getting updated. Can someone please provide some guidance on what I may be doing wrong

As folks commented, you're constructing a string that looks like a list here:

    for b in my_dict['Statement']:
        updated_arn = f"['{b['Principal']['Federated']}', {new_arn}]"
        b['Principal']['Federated']: updated_arn

You can create a real list instead:

    for b in my_dict['Statement']:
        updated_arn = [b['Principal']['Federated'], new_arn]
        b['Principal']['Federated'] = updated_arn
        # note it's `=` not `:` here

Edit: If Federated is sometimes a string, and sometimes already a list, you'll need to check its type and act accordingly:

    for b in my_dict['Statement']:
        federated = b['Principal']['Federated']
        if isinstance(federated, list):
            federated.append(new_arn)
            # (which updates the list within the dict, no need to assign back to the dict)
        else:
            b['Principal']['Federated'] = [federated, new_arn]

You can append to the current value of "Federated" as follows:

for b in my_dict['Statement']:
       b['Principal']['Federated'].append(new_arn)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM