0

I would like to construct a dynamic json and do a post call. I am using ${data.list.name} to get the value from an object but it is not working. Is there a way to do this?

function callTeams (data) {
  fetch(WEBHOOK, {
    'method': 'POST',
    'headers': { 'Content-Type': 'application/json' },
    'body': JSON.stringify({
    "@type": "MessageCard",
    "@context": "http://schema.org/extensions",
    "themeColor": "0076D7",
    "sections": [{
        "activityTitle": "${data.list.name} is valid",
        "activityImage": "https://teamsnodesample.azurewebsites.net/static/img/image5.png",
        "facts": [  {
            "name": "Key",
            "value": "${data.list.name}"
        }],
        "markdown": true
    }],
    "potentialAction": [ {
        "@type": "OpenUri",
        "name": "Submit",
        "targets": [{
            "os": "default",
            "uri": "https://learn.microsoft.com/outlook/actionable-messages"
        }]
    }]
})
  })
}
3
  • use basic string concatenation or template literals Commented Oct 11, 2022 at 17:04
  • why you wrapping name with "${...}"? Past value directly if it is string or use `` Commented Oct 11, 2022 at 17:05
  • I tried with both double quote and single quote but it is not working. Commented Oct 12, 2022 at 5:55

1 Answer 1

2

Use template literals. Replace things like

{
   "activityTitle": "${data.list.name} is valid"
}

with

{
   "activityTitle": `${data.list.name} is valid`
}

In other words, use backticks (`)

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

3 Comments

Thanks for your response. tried with both double quote and single quote but it is not working. I got response like $ is valid.
It's neither double quote or single quote, it's the ` character
ohh yeah. it worked with ` character. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.