Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.