- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2025 07:45 AM
Hi Everyone,
I am stuck while parsing the xml response.
Xml Resonse =
RESPONSE | <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <POSServices xmlns="http://pos.odc.vzwcorp.com"> <ServiceHeader> <userName>CETOMPOS</userName> <password>*********</password> <clientAppName>ATG-ECOM-DESK</clientAppName> <clientAppUserName>ECOM</clientAppUserName> <serviceName>inventoryLookup</serviceName> <serviceAction>retrieveItemQuantityByLocation</serviceAction> <applicationResponseCode>99000</applicationResponseCode> <applicationResponseClass>SUCCESS</applicationResponseClass> <applicationResponseMessage>[Wsrv15:vipsvcs06:06]Transaction processed successfully.</applicationResponseMessage> <applicationResponseDetails></applicationResponseDetails> <timeStamp>2025-02-17T17:30:08.879-05:00</timeStamp> <storeID>0</storeID> <clientAddlInfo>900000011</clientAddlInfo> <clientReferenceNumber>900000011</clientReferenceNumber> <transactionId>89885_1</transactionId> <sessionID>89885</sessionID> <serviceAreaZip>30329</serviceAreaZip> <clientOrderInfo> <channelId>B2C</channelId> <vendorId>VZWCOM</vendorId> <clientOrderType></clientOrderType> </clientOrderInfo> <responseFormat>XML</responseFormat> <atgCartId></atgCartId> <loggingId>c4ea7a390135aae5</loggingId> </ServiceHeader> <ServiceBody> <bulkItem> <response> <itemCodes> <itemCount>3</itemCount> <item> <quantity>12</quantity> <itemCode>OCPO3MAPL13128GN</itemCode> <quantityAllocated>0</quantityAllocated> </item> <item> <quantity>266</quantity> <itemCode>OCPO3MAPL13128PK</itemCode> <quantityAllocated>0</quantityAllocated> </item> <item> <quantity>98</quantity> <itemCode>RTSAMS901128K</itemCode> <quantityAllocated>0</quantityAllocated> </item> </itemCodes> </response> </bulkItem> </ServiceBody> </POSServices> |
I tried using the below code to parse the response in the flow action script step
I am getting the itemCount but 'var item = items.item(i);' returns null value. Therefore unable to fetch the item details.
This is where I am stuck right now
Can anyone help me on this
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2025 10:42 AM
Hello @jitusingh
Better to convert xml to JSON and it will be way too easier for you to get the desired results:
Background Script:
// XML response as a variable
var xmlResponse = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<POSServices xmlns="http://pos.odc.vzwcorp.com">
<ServiceHeader>
<serviceName>inventoryLookup</serviceName>
</ServiceHeader>
<ServiceBody>
<bulkItem>
<response>
<itemCodes>
<itemCount>3</itemCount>
<item>
<quantity>12</quantity>
<itemCode>OCPO3MAPL13128GN</itemCode>
</item>
<item>
<quantity>266</quantity>
<itemCode>OCPO3MAPL13128PK</itemCode>
</item>
<item>
<quantity>98</quantity>
<itemCode>RTSAMS901128K</itemCode>
</item>
</itemCodes>
</response>
</bulkItem>
</ServiceBody>
</POSServices>`;
// Remove namespace before converting XML to JSON
xmlResponse = xmlResponse.replace(/xmlns="[^"]+"/g, '');
// Convert XML to JSON
var jsonPayload = gs.xmlToJSON(xmlResponse);
gs.info("Converted JSON: " + JSON.stringify(jsonPayload, null, 2));
// Navigate the JSON structure
var items = jsonPayload.POSServices.ServiceBody.bulkItem.response.itemCodes.item;
// Ensure items exist
if (!items) {
gs.error("No items found in JSON payload.");
} else {
// scenario where there is a single item
if (!Array.isArray(items)) {
items = [items]; // Convert to an array
}
gs.info("Total items found: " + items.length);
// Loop through items and print details
for (var i = 0; i < items.length; i++) {
var itemCode = items[i].itemCode;
var quantity = items[i].quantity;
gs.info("Processing Item Code: " + itemCode + ", Quantity: " + quantity);
}
}
Results:
Hope that helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2025 10:42 AM
Hello @jitusingh
Better to convert xml to JSON and it will be way too easier for you to get the desired results:
Background Script:
// XML response as a variable
var xmlResponse = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<POSServices xmlns="http://pos.odc.vzwcorp.com">
<ServiceHeader>
<serviceName>inventoryLookup</serviceName>
</ServiceHeader>
<ServiceBody>
<bulkItem>
<response>
<itemCodes>
<itemCount>3</itemCount>
<item>
<quantity>12</quantity>
<itemCode>OCPO3MAPL13128GN</itemCode>
</item>
<item>
<quantity>266</quantity>
<itemCode>OCPO3MAPL13128PK</itemCode>
</item>
<item>
<quantity>98</quantity>
<itemCode>RTSAMS901128K</itemCode>
</item>
</itemCodes>
</response>
</bulkItem>
</ServiceBody>
</POSServices>`;
// Remove namespace before converting XML to JSON
xmlResponse = xmlResponse.replace(/xmlns="[^"]+"/g, '');
// Convert XML to JSON
var jsonPayload = gs.xmlToJSON(xmlResponse);
gs.info("Converted JSON: " + JSON.stringify(jsonPayload, null, 2));
// Navigate the JSON structure
var items = jsonPayload.POSServices.ServiceBody.bulkItem.response.itemCodes.item;
// Ensure items exist
if (!items) {
gs.error("No items found in JSON payload.");
} else {
// scenario where there is a single item
if (!Array.isArray(items)) {
items = [items]; // Convert to an array
}
gs.info("Total items found: " + items.length);
// Loop through items and print details
for (var i = 0; i < items.length; i++) {
var itemCode = items[i].itemCode;
var quantity = items[i].quantity;
gs.info("Processing Item Code: " + itemCode + ", Quantity: " + quantity);
}
}
Results:
Hope that helps!