pythonjsonnewlinezeptomail

Adding variables to an API payload with new lines in Python


I am pretty new to Python, and what I'm trying to achieve is to add variables to the payload below in Python; it's meant for Zeptomail API, and I'm trying to customize and add user-specific data to the payloads 'merge info' and the 'to' header before sending the email template.

example.py

test_name = 'users name'
test_email = 'users@email.test'
test_title = 'users title'

payload = '''
    {
        \n\"mail_template_key\":\"xxxxxxxxxxxxxxxxxxxxxxxxx\",

        \n\"from\": {
            \"address\": \"example@mail.com\",
            \"name\": \"example\"
            },

        \n\"to\": [{
            \"email-address\": {
                \"address\": \"test_email\",
                \"name\": \"test_name\"
                }
            }],

        \n\"merge_info\": {
            "name":"test_name",
            "title":"test_title"
            }}
'''

I'm expecting to print out a new payload variable with the included user-specific, 'test_' values in place.

expected print(),

payload = '''
    {
        \n\"mail_template_key\":\"xxxxxxxxxxxxxxxxxxxxxxxxx\",

        \n\"from\": {
            \"address\": \"example@mail.com\",
            \"name\": \"example\"
            },

        \n\"to\": [{
            \"email-address\": {
                \"address\": \"users@email.test\",
                \"name\": \"users name\"
                }
            }],

        \n\"merge_info\": {
            "name":"users name",
            "title":"users title"
            }}
'''

Solution

  • The payload is JSON-formatted, so simply build a Python object of dicts/lists, then convert to a JSON string:

    import json
    
    test_name = 'users name'
    test_email = 'users@email.test'
    test_title = 'users title'
    
    data = {'mail_template_key': 'xxxxxxxxxxxxxxxxxxxxxxxxx',
            'from': {'address': 'example@mail.com',
                     'name': 'example'},
            'to': [{'email-address': {'address': test_email,
                                      'name': test_name}}],
            'merge_info': {'name': test_name,
                           'title': test_title}}
    
    payload = json.dumps(data, indent=2)  # indent for pretty-printing, an API doesn't need it.
    print(payload)
    

    Output:

    {
      "mail_template_key": "xxxxxxxxxxxxxxxxxxxxxxxxx",
      "from": {
        "address": "example@mail.com",
        "name": "example"
      },
      "to": [
        {
          "email-address": {
            "address": "users@email.test",
            "name": "users name"
          }
        }
      ],
      "merge_info": {
        "name": "users name",
        "title": "users title"
      }
    }
    

    If you need to update the existing payload string you have, convert it from JSON, patch the fields, and convert it back to JSON:

    import json
    
    test_name = 'users name'
    test_email = 'users@email.test'
    test_title = 'users title'
    
    payload = '''
        {
            \n\"mail_template_key\":\"xxxxxxxxxxxxxxxxxxxxxxxxx\",
    
            \n\"from\": {
                \"address\": \"example@mail.com\",
                \"name\": \"example\"
                },
    
            \n\"to\": [{
                \"email-address\": {
                    \"address\": \"users@email.test\",
                    \"name\": \"users name\"
                    }
                }],
    
            \n\"merge_info\": {
                "name":"users name",
                "title":"users title"
                }}
    '''
    
    data = json.loads(payload)
    data['to'][0]['email-address']['address'] = test_email
    data['to'][0]['email-address']['name'] = test_name
    data['merge_info']['name'] = test_name
    data['merge_info']['title'] = test_title
    
    payload = json.dumps(data, indent=2)
    print(payload)  # same output as above