You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Power Automate Flow中生成带动态属性的复杂JSON载荷

Solution for Generating API Payload in Power Automate

Low-Code Approach Using Standard Actions

Follow these steps to build the flow without custom scripts:

  1. Trigger: Use When a row is added action targeting new_demotable001.
  2. List Mapping Rows: Add List rows action for new_metadatatable to retrieve all mapping entries.
  3. Filter Relevant Entries: Use Filter array action with the mapping rows as input. Set the filter condition to:
    @not(equals(triggerOutputs()?['body'][item()?['new_dynamicsattributename']], null))
    
    This keeps only mapping entries where the corresponding Dynamics attribute exists in the trigger data.
  4. Initialize Variable: Create a variable named dataObject with type Object and initial value {}.
  5. Apply to Each: Loop over the filtered mapping entries. Inside the loop:
    • Add a Switch action based on items('Apply_to_each')?['new_sirionattributetype'].
    • Case TEXT/NUMERIC:
      • Use Compose to create a key-value object:
        {
          "@{items('Apply_to_each')?['new_sirionattributename']}": "@{triggerOutputs()?['body'][items('Apply_to_each')?['new_dynamicsattributename']]}"
        
      • Update dataObject using Set variable with expression:
        @union(variables('dataObject'), outputs('Compose_TEXT_NUMERIC'))
        
    • Case SINGLE_REF:
      • Use Compose to create the reference object:
        {
          "@{items('Apply_to_each')?['new_sirionattributename']}": {
            "id": @{triggerOutputs()?['body'][items('Apply_to_each')?['new_dynamicsattributename']]}
          }
        
      • Update dataObject with the union expression as above.
    • Case MULTI_REF:
      • Use Split action to split the trigger value by commas:
        @split(triggerOutputs()?['body'][items('Apply_to_each')?['new_dynamicsattributename']], ',')
        
      • Use Select action to map each split value to an ID object:
        • Map key id to expression @int(item())
      • Use Compose to create the array property:
        {
          "@{items('Apply_to_each')?['new_sirionattributename']}": @{outputs('Select')}
        
      • Update dataObject with the union expression.
  6. Final Payload: Use Compose to wrap the dataObject into the final structure:
    {
      "data": @{variables('dataObject')}
    }
    

Script-Based Approach (Run JavaScript)

For a more concise implementation, use the Run JavaScript action (requires premium license):

  1. Add the Run JavaScript action with the following code:
    const triggerData = workflowContext.trigger.outputs.body;
    const mappingData = workflowContext.actions['List_rows'].outputs.body.value;
    
    // Filter relevant mapping entries
    const filteredMappings = mappingData.filter(mapping => 
      triggerData.hasOwnProperty(mapping.new_dynamicsattributename)
    );
    
    const data = {};
    
    filteredMappings.forEach(mapping => {
      const dynAttrValue = triggerData[mapping.new_dynamicsattributename];
      const sirionAttrName = mapping.new_sirionattributename;
    
      switch(mapping.new_sirionattributetype) {
        case 'TEXT':
        case 'NUMERIC':
          data[sirionAttrName] = dynAttrValue;
          break;
        case 'SINGLE_REF':
          data[sirionAttrName] = { id: dynAttrValue };
          break;
        case 'MULTI_REF':
          data[sirionAttrName] = dynAttrValue.split(',').map(id => ({ id: parseInt(id) }));
          break;
      }
    });
    
    return { data: data };
    
  2. The output of this action will directly be the required JSON payload.

Notes:

  • For the script approach, replace 'List_rows' with the actual name of your List rows action.
  • Adjust parseInt to parseFloat if the API expects floating-point numeric values.

内容的提问来源于stack exchange,提问作者Aiden Martin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 15:42:34