Skip to content

Instantly share code, notes, and snippets.

@copethomas
Created March 29, 2020 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save copethomas/9891c9f3f1b226e1e738b4926813974a to your computer and use it in GitHub Desktop.
Save copethomas/9891c9f3f1b226e1e738b4926813974a to your computer and use it in GitHub Desktop.
A IBM Datapower gateway script to convert XML to JSON
/*
Gatewayscript file for IBM Datapower
This file converts XML to JSON using Datapowers builtin "json-xml-converter module" (Firmware 7.5.0 and Above)
Example Datapower flow using MPGW:
Client -> Convert Query Params to XML -> *this gatewayscript file* -> Parse Action (Object Reference set to JSON) -> Validate using JSON Schema -> Server
*Note: Output JSON is in "BadgerFish" Format*
Helpfull Links:
https://www.ibm.com/support/knowledgecenter/SS9H2Y_7.5.0/com.ibm.dp.doc/json-xml-converter_js.html#converter.toJSON
http://badgerfish.ning.com/
Created by Thomas Cope
*/
//------------------------------------
var converter = require('json-xml-converter');
var hm = require('header-metadata');
session.input.readAsXML(function (error, xml) {
if (error) {
session.reject("Document contains Invalid XML. Can not convert XML to JSON with Invalid XML.");
}else{
var xmlString = XML.stringify(xml);
var xmlDOM = XML.parse(xmlString);
console.debug("Converting XML to JSON");
var json = converter.toJSON("badgerfish", xmlDOM)
var headers = hm.current;
headers.set('Content-Type', 'application/json');
session.output.write(json);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment