Skip to content

Instantly share code, notes, and snippets.

Created March 15, 2021 20:14
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
A node utility class to convert xml to json and vice versa. Depends on xml2js and jsontoxml node package
const jsonxml = require("jsontoxml");
const parseString = require("xml2js").parseString;
const { promisify } = require("util");
const promisfiedParseString = promisify(parseString);
module.exports = class Parser {
static parseJSONBodyToXML(jsonArgument) {
return jsonxml(jsonArgument, { html: true });
static async convertXMLToJSON(xmlMessage) {
const options = { trim: true, explicitArray: false, explicitRoot: false };
return promisfiedParseString(xmlMessage, options);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment