Search code examples
javascriptecmascript-6amazon-ses

Inserting javascript variables into escaped JSON string


In order to pass template variables to Amazon SES, templateData needs to be in the form of a string with escaped quotes, as the following:

"TemplateData": "{ \"subject\": \"mySubject\", \"date\": \"myDate\", \"header\": \"myHeader\", \"message\": \"myMessage\" }"

I need to pass data from a firestore document into these template values. I have tried using ES6's Template strings but the string is not being accepted as valid:

"TemplateData": `{ \"subject\": \"${createdData.subject}\", \"date\": \"${createdData.date}\", \"header\": \"${createdData.header}\", \"message\": \"${createdData.message}\" }`

Any ideas?


Solution

  • This should do it.

    const createdData = {
    subject: '1',
    date: '2',
    header: '3',
    message: '4'
    }
    
    const string = JSON.stringify(createdData)
    const escapedString = JSON.stringify(string)