Integration Blog Posts
cancel
Showing results for 
Search instead for 
Did you mean: 
PrasannaKumar_N
Newcomer
2,376

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.

Extracting Variables from XML Data:

Step 1: Create a Proxy

  1. Open the Proxy and navigate to the Policy section.

PrasannaKumar_N_1-1740641303935.png

 

PrasannaKumar_N_0-1740641107064.png

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.

Step 2: Configure the Extract Variable Policy

In this scenario, we select Proxy Endpoint and attach the Extract Variable policy by clicking “+” on the right side.PrasannaKumar_N_2-1740642029440.png

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.

PrasannaKumar_N_3-1740642555143.png

A sample code snippet will be generated upon adding the policy.

PrasannaKumar_N_4-1740642587044.png

 

Extract Variable Policy for XML:

 

 

<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.

  • idoc_no: Extracted using the string type to read the exact value inside idoc_no.
  • order_no: Extracted using the string type to capture the exact value inside order_no.
  • item: Extracted using the type as “nodeset” to capture the entire item node along with all its child elements. including line items and their attributes, are read and passed as a complete structure.

Refer to the SAP Documentation: SAP APIM Documentation. to modify the policy as per your requirement.

Step 3: Assign Message Policy

After extracting variables, use the Assign Message policy to store and retrieve values. you can use any other policy as per your requirement.

PrasannaKumar_N_0-1740648952935.png

 

Sample Assign Message Policy for XML:

 

 

<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> 

 

 

 

Step 4: Testing the XML Extraction

  1. Deploy the API Proxy.

  2. Copy the API Proxy URL and test it using Postman.

PrasannaKumar_N_1-1740649361335.png

PrasannaKumar_N_2-1740649866809.png

  1. Pass the Content-Type as application/xml (mandatory) in the Headers.

  2. 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>

 

Extracting Variables from JSON Data

Step 1: Configure the Extract Variable Policy for JSON

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.

PrasannaKumar_N_5-1740653924998.png

 

 

 

<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>

 

 

 

Step 2: Assign Message Policy for JSON

PrasannaKumar_N_6-1740653986609.png

 

 

 

 

<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>

 

 

Step 3: Testing the JSON Extraction

  1. Deploy the API Proxy.

  2. Copy the API Proxy URL and test it using Postman.

  3. Pass the Content-Type as application/json (mandatory).

PrasannaKumar_N_7-1740654290588.png

 

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"
                        }
                    ]
                }
            ]
        }
    ]
}

 

 

 

Conclusion

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:

I hope this guide was helpful. Feel free to leave any feedback or questions in the comments. Happy learning!