Home > other >  Union for dictionaries at a key
Union for dictionaries at a key

Time:01-27

I have two dictionaries that I'm trying to perform a union based on a key within the "dta" dictionary.

dta = {
        "msg": {
            "success": "This was a successful email sent on 01/26/2022 at 11:44 AM"
        },
        "detailType": {
            "user_info": {
                "user_email": "[email protected]",
                "user_name": "username",
            },
            "payload-info": {
                "schema": {
                    "other_emails": "[email protected]",
                    "subject": "email subject line",
                    "body": "this is the body of the email"
                }
            }
        }
    }

other_data = {
    "other_emails": "[email protected]",
    "subject": "The original email subject line",
    "body": "Original email body",
    "reply": "[email protected]"
}

And I would like to do a union on the "schema" key of dta. However when I try this

if "detailType" in dta:
    combined_data = dta | other_data
    print(combined_data)

This is my result

{
    "msg": {"success": "This was a successful email sent on 01/26/2022 at 11:44 AM"},
    "detailType": {
        "user_info": {
              "user_email": "[email protected]", 
               "user_name": "username"
             },
        "payload-info": {
            "schema": {
                "other_emails": "[email protected]",
                "subject": "email subject line",
                "body": "this is the body of the email",
            }
        },
    },
    "other_emails": "[email protected]",
    "subject": "The original email subject line",
    "body": "Original email body",
    "reply": "[email protected]",
}

However, I'm trying to get this as my result

{
    'msg': {'success': 'This was a successful email sent on 01/26/2022 at 11:44 AM'},
    'detailType': {
        'user_info': {
            'user_email': '[email protected]',
            'user_name': 'username'
        },
        'payload-info': {
            'schema': {
                'other_emails': '[email protected]',
                'subject': 'The original email subject line',
                'body': 'Original email body',
                'reply': '[email protected]'
            }
        }
    }
}

Is there a way to do a union using a key as the starting place?

CodePudding user response:

You're merging other_data with the top-level dta dictionary. You should be merging it with dta['detailType']['payload-info']['schema']. So use:

if "detailType" in dta:
    dta['detailType']['payload-info']['schema'].update(other_data)

CodePudding user response:

Another way to achieve the same result.

if "detailType" in dta:
    dta["detailType"]["payload-info"]["schema"] = {**dta["detailType"]["payload-info"]["schema"], **other_data}

Output:

{
    'msg': {
        'success': 'This was a successful email sent on 01/26/2022 at 11:44 AM'
    },
    'detailType': {
        'user_info': {
            'user_email': '[email protected]',
            'user_name': 'username'
        },
        'payload-info': {
            'schema': {
                'other_emails': '[email protected]',
                'subject': 'The original email subject line',
                'body': 'Original email body',
                'reply': '[email protected]'
            }
        }
    }
}
  •  Tags:  
  • Related