如何在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:
- Trigger: Use When a row is added action targeting
new_demotable001. - List Mapping Rows: Add List rows action for
new_metadatatableto retrieve all mapping entries. - Filter Relevant Entries: Use Filter array action with the mapping rows as input. Set the filter condition to:
This keeps only mapping entries where the corresponding Dynamics attribute exists in the trigger data.@not(equals(triggerOutputs()?['body'][item()?['new_dynamicsattributename']], null)) - Initialize Variable: Create a variable named
dataObjectwith type Object and initial value{}. - 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
dataObjectusing Set variable with expression:@union(variables('dataObject'), outputs('Compose_TEXT_NUMERIC'))
- Use Compose to create a key-value object:
- 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
dataObjectwith the union expression as above.
- Use Compose to create the reference object:
- 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
idto expression@int(item())
- Map key
- Use Compose to create the array property:
{ "@{items('Apply_to_each')?['new_sirionattributename']}": @{outputs('Select')} - Update
dataObjectwith the union expression.
- Use Split action to split the trigger value by commas:
- Add a Switch action based on
- Final Payload: Use Compose to wrap the
dataObjectinto the final structure:{ "data": @{variables('dataObject')} }
Script-Based Approach (Run JavaScript)
For a more concise implementation, use the Run JavaScript action (requires premium license):
- 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 }; - 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
parseInttoparseFloatif the API expects floating-point numeric values.
内容的提问来源于stack exchange,提问作者Aiden Martin
相关产品推荐
相关产品推荐

