Parse XML -> JSON, then pass full contents to webhook

Hi!

Beginner here. I need to connect one app to my CRM and pull all the data possible so I can pass it to a webhook (via Zapier). However, the results are XML, but the webhook needs them in JSON.

I have scoured here and StackOverflow and found a lot of OK code, but none seems to do what I need to do.

Here’s a sample of the XML:

<?xml version="1.0" encoding="utf-8"?>
<Events>
    <Link rel="link">
        <Event>
            <EventID>1</EventID>
            <UniqueIdentifier>6249a570-8484-4f7d-b22d-a55680d7de9b</UniqueIdentifier>
            <Code>SKU-001</Code>
            <Name>Event Name</Name>
            <StartDateTime>2023-06-04T10:00:00.0000000-04:00</StartDateTime>
            <FinishDateTime>2023-06-08T15:30:00.0000000-04:00</FinishDateTime>
            <StartTimeZoneAbbr>EDT</StartTimeZoneAbbr>
            <FinishTimeZoneAbbr>EDT</FinishTimeZoneAbbr>
            <LocationName>New York</LocationName>
            <IsPrivate>false</IsPrivate>
            <Status>Completed</Status>
            <CreatedDateTime>2023-03-28T20:03:28.5133495Z</CreatedDateTime>
            <LastModifiedDateTime>2023-06-12T14:37:45.9044154Z</LastModifiedDateTime>
            <Link rel="self" type="application/xml" href="link"/>
            <Link rel="self" type="application/xml" href="link"/>
        </Event>
    </Link>

And here’s what I can get using xml2json:

Any help on:

  • Converting this XML to JSON
  • Passing all of it to a POST request also in Postman

Would be much appreciated.

Thanks!

xml2js is also available in the Postman sandbox and gives you a lot more control over the output.

For example…

let respBodyXML = pm.response.json().data;

let parseString = require('xml2js').parseString;

parseString(respBodyXML, {
    ignoreAttrs: true, 
    explicitArray: false,
}, function (err, result) {
    console.log(result);
});

image

Have a look here (scroll to the bottom for all of the formatting options).

xml2js - npm (npmjs.com)

With regards to your webhook, you need to provide an example JSON structure that the webhook will accept.

You have 67 links in your screenshot, so I suspect you will need to loop through the JSON produced by the above code, and retrieve the details you need to create a new JSON object in the correct structure for your webhook.

If you need help with this, please provide details on what the webhook will accept and an XML example with more than one link.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.