XML-JSON Operations on Pipedream

Transform XML to JSON seamlessly with Node.js in Pipedream for real-time data processing.

Ceyhun Enki Aksan
Ceyhun Enki Aksan Entrepreneur, Maker

In my previous post, I discussed an integration platform named Pipedream, and explained the types of purposes it serves and how it can be used. Previously, we had examined the process of reading an XML file with PHP and sending the result to an endpoint, and now we’re revisiting it using Node.js.

In that example, the xml2js and express.js modules were used. In this article, we’ll implement the entire process using different modules through Pipedream.

Pipedream

For those who haven’t read the article about Pipedream or who are arriving directly at this post, let’s provide a brief overview of Pipedream. Pipedream is an integration platform that enables you to create workflows to connect applications and APIs without requiring server or infrastructure constraints. Numerous pre-built application connections can be integrated into these workflows. Additionally, operations between applications can be customized through code. The coding process is performed using JavaScript within a Node.js foundation, and modules can be easily added to workflows.

XML-to-JSON Processing via Pipedream

In the article titled XML-to-JSON Operations with Node.js, we converted an XML source (URL and/or file) into a JSON output. Instead of repeating this same process, let’s approach it differently. First, we’ll replace the modules we used previously. Then, we’ll execute this operation via the command line.

The required usage is as follows:

curl -d '{
  "xml": "<xml-url>",
  "to": "<target-url>"
}' \
  -H "Content-Type: application/json" \
  <pipedream-url>

Important Rules:

  1. Maintain the original formatting (markdown, HTML tags, links, etc.)
  2. Keep technical terms and proper nouns as appropriate
  3. Preserve code blocks and technical syntax exactly
  4. Maintain the same tone and style
  5. Only output the translated text, no explanations or comments

We are passing two values to the target via curl: <pipedream-url>. We are sending "xml": "<xml-url>" and "to": "<target-url>". The xml value refers to the XML path we have (which could also be a file), while the to value specifies the URL where the JSON output resulting from the XML conversion should be sent.

Previously, in our XML examples, we had used node definitions. This time, we’ll proceed with an example that includes attribute definitions. I’ll use an XML source I found for the xml-url. We can generate different XML content using the store IDs available on the website.

For the target-url, I’ll be using webhook.site.

Next up is creating a Pipedream workflow. Naturally, as required by our application, we must first create an HTTP/Webhook trigger for pipedream-url.

Pipedream HTTP/Webhook
Pipedream HTTP/Webhook

With these details, we have now completed the missing fields of our command.

curl -d '{
  "xml": "https://www.haydigiytoptan.com/FaprikaXml/DG36SX/1/",
  "to": "https://webhook.site/ea994303-0ab3-4506-8b33-0f31f8cdb339"
}' \
  -H "Content-Type: application/json" \
  https://en5bxyd2ehwly83.m.pipedream.net

Now we can proceed to the next step. When we execute the above command, Pipedream receives two pieces of information—xml and to—to its target. However, these pieces of information naturally undergo encoding. Therefore, we must decode them back to their original form. As a result, we should add a base64_decode_string step as the next action in the workflow.

Decoded UTF-8 string
Decoded UTF-8 string

Now we can proceed to the final step and send the obtained data to the specified target. The modules we’ll use are fast-xml-parser1 and axios2.

const parser = require('fast-xml-parser');
const axios = require('axios');

We now have a decoded string value.

{
  "xml": "https://www.haydigiytoptan.com/FaprikaXml/DG36SX/1/",
  "to": "https://webhook.site/fe1f0a11-0cb5-4a60-bb79-43d68d14badb"
}

We need to convert this back into JSON format.

let XML_data = JSON.parse(steps.base64_decode_string.data);

The next step involves retrieving the XML file and converting it into JSON format.

const parser = require('fast-xml-parser');
const axios = require('axios');

let XML_data = JSON.parse(steps.base64_decode_string.data);

const XML_url = XML_data.xml;

let XML_file = (await axios({
  method: "GET",
  url: XML_url,
}));

const options = {
    attributeNamePrefix : "",
    attrNodeName: "attr",
    textNodeName : "#text",
    ignoreAttributes : false,
    ignoreNameSpace : false,
    allowBooleanAttributes : false,
    parseNodeValue : true,
    parseAttributeValue : true,
    trimValues: true,
    arrayMode: false
};

if( parser.validate(XML_file.data) === true) {
  try{
    var jsonObj = parser.parse(XML_file.data, options, true);
  }catch(error){
    console.log(error.message)
  }
}

await axios({
  method: 'post',
  url: XML_data.to,
  data: jsonObj.Products
});

$respond({
  body: 'OK'
})

When we execute the command specified above from the command line, the XML content will be transformed into JSON format and sent to the specified target. After the completion of the process, you will receive an “OK” response in the console. Now, let’s check our target URL.

Yes, the code we sent via the command line appears to have been successfully processed. You can access the relevant workflow under the name XML-JSON Command3. Finally, fast-xml-parser1 operates according to the specified options. Therefore, you may need to adjust these fields depending on your XML content. For more details, please refer to the GitHub4 and the example application5 page.

Footnotes

  1. fast-xml-parser. NPM 2
  2. axios/axios. Promise based HTTP client for the browser and node.js. GitHub
  3. XML-JSON Command. Pipedream
  4. NaturalIntelligence/fast-xml-parser. GitHub
  5. Fast XML Parser. GitHub