Sending XML Payload and Converting XML Response to JSON with Node.js
- By : Mydatahack
- Category : Data Engineering, Data Ingestion
- Tags: Node.js, QAS, XML
Here is the quick Node.js example of interacting with a rest API endpoint that takes XML string as a payload and return with XML string as response. Once we get the response, we will convert it to a JSON object.
For this example, we will use the old-school QAS (Quick Address Search). Although Experian moved to a more modern endpoint EDQ (Experian Data Quality), the endpoint is still available. Note that you need to sign up for a license. The purpose of this post is just to show how to interact with a rest API that uses XML. You can use this example for your own use case.
Firs of all, we use two dependencies, axios for making API calls and xml2js for converting xml string to JSON. Make sure that the xml string doesn’t contain any new line.
npm i axios xml2js
Then, you can pass xml as a string in the payload. When the response comes back as a xml string, you can convert it to JSON.
That’s it. Pretty easy!