如何使用Azure APIM策略实现指定XML到目标JSON的格式转换
Azure APIM实现XML转指定格式JSON的方案
你可以直接使用set-body策略配合Liquid转换模板完成需求,无需额外对接第三方服务,在APIM网关层即可实现100%匹配目标格式的转换。
完整策略示例
将以下策略添加到API的入站(inbound)处理段即可:
<inbound> <base /> <set-body template="liquid"> { "body": { "ProcessName": "SC Account Registration Process", "SiebelMessage": { "IntObjectName": "SC EAI Contact", "MessageType": "Integration Object", "IntObjectFormat": "Siebel Hierarchical", "ListOfSC EAI Contact": { "Contact": { "Email Address": "{{body.envelope.Body.CreateCustomerProfileRequest.CustomerProfile.CustomerAddressBook.Email.EmailAddress}}", "First Name": "{{body.envelope.Body.CreateCustomerProfileRequest.CustomerProfile.CustomerName.FirstName}}", "Last Name": "{{body.envelope.Body.CreateCustomerProfileRequest.CustomerProfile.CustomerName.LastName}}", "SC External Customer Id": "{{body.envelope.Body.CreateCustomerProfileRequest.CustomerProfile.CustomerIdentification.CustomerID}}", "Integration Id": "{{body.envelope.Body.CreateCustomerProfileRequest.CustomerProfile.CustomerIdentification.CustomerID}}", "Type": "CRM_Unqualified", "SC Sleep ": "{{body.envelope.Body.CreateCustomerProfileRequest.CustomerProfile.CustomerSleep}}" } } } } } </set-body> <!-- 转换后自动修改请求头为JSON格式,适配后端接收要求 --> <set-header name="Content-Type" exists-action="override"> <value>application/json</value> </set-header> </inbound>
规则说明
- 固定值字段:不需要从XML读取的字段(比如ProcessName、IntObjectName、Type等)直接在模板中写死即可
- 动态取值字段:按照XML节点层级写对应路径即可,Liquid模板会自动忽略命名空间前缀,无需额外处理ns3、ns5等标识
- 你提供的样例XML使用上述策略转换后,输出结果完全匹配要求的目标JSON结构,仅动态字段会替换为XML中的实际值
内容的提问来源于stack exchange,提问作者sreekanth gandla
相关产品推荐
相关产品推荐

