0

I'm using AWS SES send_templated_email method. It's using a parameter called TemplateData TemplateData – An escaped JSON string that contains key-value pairs. The keys correspond to the variables in the template (for example, {{name}}). The values represent the content that replaces the variables in the email.

If I use it hard coded, like this:

TemplateData="""{\"quarter_num\":\"Q2\",
                  \"year\":\"2021\",
                }"""

This works

But if I want to use variables in that string, something like:

TemplateData="""{\"quarter_num\":\"{}\",
                  \"year\":\"{}\",
                    }""".format("Q2", "2021")

This won't work. I think it's an escaping problem but I can't figure out how to do it correctly. This is the error message I got:

[ERROR] KeyError: '"quarter_num"'
Traceback (most recent call last):
  File "/var/task/email_reports.py", line 35, in email_users
    }""".format("Q2", "2021")

When I use the hard coded string, this works without any problem.

Lykosz
  • 87
  • 1
  • 6

2 Answers2

2

Don't try to write it as a string with interpolated variables. That way lies madness. Write a Python dict, and convert that to a string using json.dumps().

TemplateData = json.dumps({"quarter_num": "Q2",
                           "year": "2021",
                          })

Then it is trivially obvious how to replace some of that data with variables. For example:

TemplateData = json.dumps({"quarter_num": "Q%s" % quarter,
                           "year": year,
                          })

This way, your IDE helps you write your dictionary, and Python itself checks your syntax, which can't happen if everything's in a big ugly string.

kindall
  • 178,883
  • 35
  • 278
  • 309
  • I just tried this. Somehow this isn't working either. I guess this has something to do with how AWS designed their APIs? It says here that parameter needs to be An escaped JSON string that contains key-value pairs. https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-personalized-email-api.html – Lykosz Jul 28 '21 at 21:51
  • 1
    @Lykosz again "it's not working" is **not an adequate problem description**. – juanpa.arrivillaga Jul 28 '21 at 21:52
  • @kindall Sorry, my bad. I think this actually solved that problem. The lambda just didn't deploy correctly. – Lykosz Jul 28 '21 at 21:55
1

You don't need to escape quotation marks in multiline string:

In [5]: TemplateData=f"""{{"quarter_num":"{"Q2"}",                                                                         
...:                       "year":"{2021}"}}"""   
Out[5]: '{"quarter_num":"Q2",\n                  "year":"2021"}'                                                                  

In this example I use f-string, you just need to double curly brackets to escape them

Thibault D.
  • 1,567
  • 10
  • 23
  • The hard coded way has always been working. What I need is to add variable into that string. – Lykosz Jul 28 '21 at 21:37
  • This is exactly what I'm doing : formatting the string to put "Q2" and "2021" at the right position. You could replace them by any expression and they would be interpolated – Thibault D. Jul 28 '21 at 22:19
  • Sorry, you are right. I didn't get it initially. I just tried, this works. – Lykosz Jul 28 '21 at 23:02