My companion blog post
I was recently asked to help accelerate a team with a Proof of Concept (PoC) that showed how they could integrate a legacy API into their modern application. The team was leveraging node.js and the legacy API returned responses in an XML format. The modern application needed the data in a JSON format.
I was quickly able to provide a working PoC, and as a result I'm going to share with you how to use an existing Node Package Manager (NPM) library to translate XML to JSON.
This blog post is how I was able to help accelerate the team.
The Backlog Item
The team currently uses node.js code to hit a legacy API that provides XML responses. The backlog item is to change from a file-based integration with downstream products to a message-based integration. The messages need to be JSON format to land on a service bus queue. The schemas between the XML and JSON are not a one-for-one match, thus requiring a slight transformation.
So the first order of business is to translate the XML response to a JSON object.
I was able to find an existing NPM library xml2js that has just over 11 million weekly downloads at the time of writting this blog. The popularity of the library made the decision easy to give it a try.
Clone the rfwr_e13 repo to start, my favorite way is to execute the following in powershell:
git clone https://github.com/RunnX/rfwr_e13.git
After cloning you have two options
- Use the main branch and go through the steps by yourself.
- Use the complete branch and just execute the complete code yourself.
Open rfwr_e13 folder in VS Code and open a new terminal window within VS Code. Execute the following in the terminal to initalize npm.
npm init -y
Add an app.js file to the rfwr_13 folder and start using my code snippets. Open the app.js file and populate the main code by typing the following.
main [tab autocomplete]
The code is using an API from the National Highway Traffic Safety Administration to get a list of models for the Tesla vehicle make. The Vehicle API does offer a JSON response but to showcase this example the XML format response is configured in the code.
Execute the following in the terminal to execute the code to show current running state.
Execute the following in the terminal to install the npm package xml2js.
npm install xml2js
The npm package here is xml2js and it allows the parsing of the XML response to return a JSON response. Doing 'npm install xml2js' installs the package.
Add a couple of snippets to the app.js file to make use of the package. Type the following at the top of the file to import the library.
rxml2js [tab autocomplete]
Add the second snippet to the bottom of the file which adds the Convert() function to the app.
convert [tab autocomplete]
Uncomment the call to the Convert() function in the main() function (should be line 18).
Finally execute the following in the terminal to execute the code to show the final running state.
After running the command the transformation from XML to JSON should show in the console.
The easier option is to switch to the complete code branch and just execute the code to show the final running state.
git switch complete npm install node app.js
Pretty great library with little to no friction to implement.
The Important Links
Checkout the Quick Hit episode of this post.
Principal Engineer | Enterprise Cloud Architect • CloudSDK | Azure DevOps | Developer Enablement • #Employee @UHG/Optum since 2007 | Views expressed are my own