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.

6
  • "This won't work" when you say something "doesn't work", please always tell us exactly what that means. Don't make people guess Commented Jul 28, 2021 at 21:28
  • Does this answer your question? How can I print literal curly-brace characters in a string and also use .format on it? Commented Jul 28, 2021 at 21:30
  • @juanpa.arrivillaga the error is a boto3 error, bascially says I'm not using the correct string for that parameter Commented Jul 28, 2021 at 21:31
  • Again, please tell us exactly what the error is. Commented Jul 28, 2021 at 21:32
  • @juanpa.arrivillaga I added the error message I got. Commented Jul 28, 2021 at 21:40

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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. docs.aws.amazon.com/ses/latest/DeveloperGuide/…
@Lykosz again "it's not working" is not an adequate problem description.
@kindall Sorry, my bad. I think this actually solved that problem. The lambda just didn't deploy correctly.
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

3 Comments

The hard coded way has always been working. What I need is to add variable into that string.
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
Sorry, you are right. I didn't get it initially. I just tried, this works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.