Home > Mobile >  How to dynamically construct json in node JS
How to dynamically construct json in node JS

Time:10-12

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"
        }]
    }]
})
  })
}

CodePudding user response:

Use template literals. Replace things like

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

with

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

In other words, use backticks (`)

  • Related