如何将D365业务事件数据映射为指定JSON数组?
如何正确从Dynamics 365业务事件解析后的JSON中取值并生成指定格式的JSON数组?
问题场景
自定义Dynamics 365业务事件后,已完成JSON解析步骤,解析输出结构如下:
{ "body": { "BillClientId": "abcdef", "BillRad": "Request", "Boxes": 0, "BusinessEventId": "SBSalesInvoicePostingBusinessEvents", "BusinessEventLegalEntity": "usmf", "comt": "", "ContextRecordSubject": "", "ControlNumber": 123456, "CustAccount": "", "Description": "", "DestAddressType": "DESTINATION", "Destcity3": "San Diego", "Destcity4": "San Diego", "DestclntName": "Contoso Retail Los Angeles", "Destcontacto": "Contoso Retail San Diego", "DestCountry": "USA", "Destdoor": "S/N", "Destemail": "contoso.retail.los.angeles@customer62.consolidatedmessenger.com", "Destphone": "123-444-555" } }
尝试用表达式@{body('Parse_JSON')?['properties']?['BillClientId']}取值时返回空,无法获取数据,需要正确的取值方法及生成指定格式JSON数组的实现方式。
错误原因
你的表达式错误引用了properties层级,但从解析后的JSON结构可以看到,所有业务数据都嵌套在顶层的body节点下,不存在properties这个层级,因此取值为空。
正确实现方法
1. 正确的取值表达式
直接通过body层级访问目标字段,示例如下:
- 获取
BillClientId:@{body('Parse_JSON')?['body']?['BillClientId']} - 获取
Destcontacto:@{body('Parse_JSON')?['body']?['Destcontacto']} - 获取
ControlNumber:@{body('Parse_JSON')?['body']?['ControlNumber']}
2. 生成指定格式的JSON数组
以构造包含核心字段的JSON数组为例,在Power Automate的Compose动作中输入以下表达式(可根据第三方服务要求调整字段映射):
[ { "ClientID": @{body('Parse_JSON')?['body']?['BillClientId']}, "RequestType": @{body('Parse_JSON')?['body']?['BillRad']}, "ControlNumber": @{body('Parse_JSON')?['body']?['ControlNumber']}, "DestinationInfo": { "City": @{body('Parse_JSON')?['body']?['Destcity3']}, "ContactName": @{body('Parse_JSON')?['body']?['Destcontacto']}, "Email": @{body('Parse_JSON')?['body']?['Destemail']}, "Phone": @{body('Parse_JSON')?['body']?['Destphone']} } } ]
3. 额外验证步骤
确保Parse JSON动作使用的Schema完全匹配业务事件输出的JSON结构,若Schema不匹配,会导致字段无法被正确识别和访问。你可以直接将解析后的JSON样本导入生成Schema,避免手动编写出错。
内容的提问来源于stack exchange,提问作者Dishanth Varadaraj
相关产品推荐
相关产品推荐

