power-automatepowerappspowerapps-canvaspower-platformpowerapps-modeldriven

How to prepare JSON payload with dynamic attributes in Power Automate Flow


I'm using two tables 'new_demotable001' and 'new_metadatatable'. One to trigger the flow and another as Mapping Sheet, respectively.

By using both I want to create a JSON payload that I'll use to call external API. Please help me create the payload.

<Details/Prerquisite>

  1. When a new row added in new_demotable001, flow triggers.
  2. The data/attributes we will receive from table are - Note: I've removed unnecessary attributes.
{
  "new_empname": "EmpName5",
  "new_demotable001id": "259ffaeb-a354-ef11-bfe3-000d3af2cd77",
  "new_empcontact": "555555555555",
  "new_name": "DemoDataTestin",
  "new_empstatus": 100000001,
  "_new_empstatus_label": "On-Boarded"
}
  1. Then fetch details from 'new_metadatatable' table using "List Rows".
  2. The data/attributes we will receive from table are - Note: I've removed unnecessary attributes.
{
  "@Microsoft.Dynamics.CRM.totalrecordcount": -1,
  "@Microsoft.Dynamics.CRM.totalrecordcountlimitexceeded": false,
  "@Microsoft.Dynamics.CRM.globalmetadataversion": "10896094",
  "value": [
    {
      "new_sirionattributename": "title",
      "new_dynamicsattributename": "new_empname"
      "new_name": "First"
    },
    {
      "new_sirionattributename": "test1",
      "new_dynamicsattributename": "new_empcontact",
      "new_name": "Second"
    },
    {
      "new_sirionattributename": "orgId",
      "new_dynamicsattributename": "new_empstatus",
      "new_name": "Third"
    }
  ]
}
  1. Now, I want to create a JSON payload using above attributes. To create payload we will use only those attributes which are present in 'new_metadatatable' and also coming from 'new_demotable001'. -> The Condition I used - contains(triggerOutputs()?['body'], items('Apply_to_each')?['new_dynamicsattributename'])
  2. For above set of data, the expected output payload should be-
{
    "data":{
            "title": "EmpName5",
            "test1": "555555555555",
            "orgId": "100000001"
           }
}


Solution

  • Try this approach: screen snip of flow

    Details:

    Step: rowDemo
    Action: Compose
    Comment: you don't need this step, just for demo, this is the first JSON.
    
    Step: metaData
    Action: Compose
    Comment: you don't need this step, just for demo, this is the second JSON.
    
    Step: selectAttributes
    Action: Select
    From: outputs('metaData')['value']
    Map left: item()?['new_sirionattributename']
    Map right: outputs('rowDemo')?[item()?['new_dynamicsattributename']]
    
    Step: payload
    Action: Compose
    Inputs:
    {
      "data": @{json(
    replace(
    replace(
    replace(
      string(body('selectAttributes')),
      '},{', ','), 
      '[', ''), 
      ']', '')
    )}
    }
    

    Notes:

    To give you final payload output of:
    screen snip of the result