In this blog, we’ll explore how to extract variables from XML and JSON data using the Extract Variable policy in SAP API Management (APIM). This policy allows you to parse data from requests or responses and assign values to variables for use during API proxy execution.
Open the Proxy and navigate to the Policy section.
2. Click Edit in the top-right corner.
3. On the left panel, select either the Proxy Endpoint or Target Endpoint where you want to add this policy.
In this scenario, we select Proxy Endpoint and attach the Extract Variable policy by clicking “+” on the right side.
Give the policy name and select the stream as Incoming or Outgoing depending on whether it should be applied to the request or response. Here, we use Incoming Request.
A sample code snippet will be generated upon adding the policy.
<ExtractVariables async="true" continueOnError="false" enabled="true" xmlns='http://www.sap.com/apimgmt'>
<XMLPayload>
<Variable name="idoc_no" type="string">
<XPath>/orders/order/header/idoc_no</XPath>
</Variable>
<Variable name="order_no" type="string">
<XPath>//order_no</XPath>
</Variable>
<Variable name="item" type="nodeset">
<XPath>//item</XPath>
</Variable>
</XMLPayload>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</ExtractVariables>
With the above policy I am extracting the below details.
Refer to the SAP Documentation: SAP APIM Documentation. to modify the policy as per your requirement.
After extracting variables, use the Assign Message policy to store and retrieve values. you can use any other policy as per your requirement.
<AssignMessage async="false" continueOnError="false" enabled="true" xmlns='http://www.sap.com/apimgmt'>
<Set>
<Payload contentType="application/xml" variablePrefix="@" variableSuffix="#">
<Orders>
<idoc_no>@idoc_no#</idoc_no>
<order_no>@order_no#</order_no>
@item#
</Orders>
</Payload>
</Set>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew="false" type="request"> request</AssignTo>
</AssignMessage>
We must specify the type as either "request" or "response" depending on where the policy is applied. Since the policy is being used on the Request here, the type is set as "request".
<AssignTo createNew="false" type="request"> request</AssignTo>
Deploy the API Proxy.
Copy the API Proxy URL and test it using Postman.
Pass the Content-Type as application/xml (mandatory) in the Headers.
Use the sample XML payload to validate the extraction.
Below is the sample Input Payload:
<?xml version="1.0" encoding="UTF-8"?>
<orders>
<order>
<header>
<idoc_no>888888</idoc_no>
<order_no>5000123456</order_no>
<sold_to>AAAA-111</sold_to>
<ship_to>BBBB-222</ship_to>
<order_date>20211201</order_date>
</header>
<item>
<line_item>00001</line_item>
<material>800111</material>
<quantity_uom>pce</quantity_uom>
<weight>20.2</weight>
<weight_uom>kgm</weight_uom>
<schedule>
<line>
<schedule_qty>10</schedule_qty>
<schedule_date>20211210</schedule_date>
</line>
</schedule>
</item>
<item>
<line_item>00002</line_item>
<material>800222</material>
<quantity_uom>pce</quantity_uom>
<weight>200.3</weight>
<weight_uom>kgm</weight_uom>
<schedule>
<line>
<schedule_qty>40</schedule_qty>
<schedule_date>20211210</schedule_date>
</line>
<line>
<schedule_qty>60</schedule_qty>
<schedule_date>20211220</schedule_date>
</line>
</schedule>
</item>
</order>
</orders>
Output Payload Received:
<Orders>
<idoc_no>888888</idoc_no>
<order_no>5000123456</order_no>
<item>
<line_item>00001</line_item>
<material>800111</material>
<quantity_uom>pce</quantity_uom>
<weight>20.2</weight>
<weight_uom>kgm</weight_uom>
<schedule>
<line>
<schedule_qty>10</schedule_qty>
<schedule_date>20211210</schedule_date>
</line>
</schedule>
</item>
<item>
<line_item>00002</line_item>
<material>800222</material>
<quantity_uom>pce</quantity_uom>
<weight>200.3</weight>
<weight_uom>kgm</weight_uom>
<schedule>
<line>
<schedule_qty>40</schedule_qty>
<schedule_date>20211210</schedule_date>
</line>
<line>
<schedule_qty>60</schedule_qty>
<schedule_date>20211220</schedule_date>
</line>
</schedule>
</item>
</Orders>In the same way we can extract variables from the JSON data as well to extract the JSON use the below format in the policy.
<ExtractVariables async="true" continueOnError="false" enabled="true" xmlns='http://www.sap.com/apimgmt'>
<JSONPayload>
<Variable name="idoc_no">
<JSONPath>$.orders[0].header.idoc_no</JSONPath>
</Variable>
<Variable name="order_no">
<JSONPath>$.orders[0].header.order_no</JSONPath>
</Variable>
<Variable name="item">
<JSONPath>$.orders[*].item</JSONPath>
</Variable>
</JSONPayload>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</ExtractVariables>
<AssignMessage async="false" continueOnError="false" enabled="true" xmlns='http://www.sap.com/apimgmt'>
<Set>
<Payload contentType="application/json" variablePrefix="@" variableSuffix="#">
{
"orders": [
{
"header": {
"idoc_no": "@idoc_no#",
"order_no": "@order_no#",
}
"item": @item#
}
]
}
</Payload>
</Set>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew="false" type="request">request</AssignTo>
</AssignMessage>
Deploy the API Proxy.
Copy the API Proxy URL and test it using Postman.
Pass the Content-Type as application/json (mandatory).
JSON Input payload Used:
{
"orders": [
{
"header": {
"idoc_no": "888888",
"order_no": "5000123456",
"sold_to": "AAAA-111",
"ship_to": "BBBB-222",
"order_date": "20211201"
},
"item": [
{
"line_item": "00001",
"material": "800111",
"quantity_uom": "pce",
"weight": 20.2,
"weight_uom": "kgm",
"schedule": [
{
"schedule_qty": 10,
"schedule_date": "20211210"
}
]
},
{
"line_item": "00002",
"material": "800222",
"quantity_uom": "pce",
"weight": 200.3,
"weight_uom": "kgm",
"schedule": [
{
"schedule_qty": 40,
"schedule_date": "20211210"
},
{
"schedule_qty": 60,
"schedule_date": "20211220"
}
]
}
]
}
]
}
JSON Output:
{
"orders": [
{
"header": {
"idoc_no": "888888",
"order_no": "5000123456",
}
"item": [
{
"line_item": "00001",
"material": "800111",
"quantity_uom": "pce",
"weight": 20.2,
"weight_uom": "kgm",
"schedule": [
{
"schedule_qty": 10,
"schedule_date": "20211210"
}
]
},
{
"line_item": "00002",
"material": "800222",
"quantity_uom": "pce",
"weight": 200.3,
"weight_uom": "kgm",
"schedule": [
{
"schedule_qty": 40,
"schedule_date": "20211210"
},
{
"schedule_qty": 60,
"schedule_date": "20211220"
}
]
}
]
}
]
}
By following this approach, you can extract elements from both XML and JSON data using the Extract Variable Policy and assign them using the Assign Message Policy in SAP API Management.
For further reference:
SAP APIM Documentation: SAP APIM
Google Apigee: Apigee Docs
I hope this guide was helpful. Feel free to leave any feedback or questions in the comments. Happy learning!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 25 | |
| 21 | |
| 12 | |
| 9 | |
| 8 | |
| 8 | |
| 6 | |
| 6 | |
| 5 | |
| 3 |