Skip to content

Instantly share code, notes, and snippets.

@shiddugmail
Last active September 11, 2020 21:56
Show Gist options
  • Save shiddugmail/e552870c30ba2e54bf9957fff0c19cbc to your computer and use it in GitHub Desktop.
Save shiddugmail/e552870c30ba2e54bf9957fff0c19cbc to your computer and use it in GitHub Desktop.
XML String to JSon using fast-xml-parser
[
{
"id": "87fc889c.4dc108",
"type": "xml",
"z": "9b34b2d0.13426",
"name": "XML to Object (No JSon)",
"property": "payload",
"attr": "",
"chr": "",
"x": 574,
"y": 460,
"wires": [
[
"8ef14803.e62838",
"7d78c49.0f8b33c"
]
]
},
{
"id": "d11f7ac8.3032c8",
"type": "inject",
"z": "9b34b2d0.13426",
"name": "XML String - BBPS - Customer OU Request",
"topic": "",
"payload": "<?xml version=\"1.0\"encoding=\"UTF-8\"?><bbps:BillFetchRequest xmlns:bbps=\"http://bbps.org/schema\"><Head ver=\"1.0\" ts=\"2015-02-16T22:02:35+05:30\" origInst=\"OU01\" refId=\"HENSVVR4QOS7X1UGPY7JGUV444PL9T2C3QM\" /><Analytics><Tag name=\"FETCHREQUESTSTART\" value=\"2015-02-16T22:02:00+05:30\" /><Tag name=\"FETCHREQUESTEND\" value=\"2015-02-16T22:02:35+05:30\" /></Analytics><Txn ts=\"2015-02-16T22:02:35+05:30\" msgId=\"8ENSVVR4QOS7X1UGPY7JGUV444PL9T2C3QX\"><RiskScores><Score provider=\"OU01\" type=\"TXNRISK\" value=\"030\" /><Score provider=\"BBPS\" type=\"TXNRISK\" value=\"030\" /></RiskScores></Txn><Customer mobile=\"9830098300\"><Tag name=\"EMAIL\" value=\"arnabmoitra@npci.org.in\" /><Tag name=\"AADHAAR\" value=\"123456789012\" /><Tag name=\"PAN\" value=\"BXXCG7754K\" /></Customer><Agent id=\"OU01AI34INT001123456\"><Device><Tag name=\"MOBILE\" value=\"9830098300\" /><Tag name=\"GEOCODE\" value=\"12.9667,77.5667\" /><Tag name=\"POSTAL_CODE\" value=\"400063\" /><Tag name=\"IP\" value=\"124.170.23.22\" /><Tag name=\"INITIATING_CHANNEL\" value=\"INT/INTB/MOB/MOBB/KIOSK/ATM/BNKBRNCH/AGT/BSC\" /><Tag name=\"TERMINAL_ID\" value=\"1234556\" /><Tag name=\"IMEI\" value=\"123456789012345\" /><Tag name=\"IFSC\" value=\"ABCD0001234\" /><Tag name=\"MAC\" value=\"00-0D-60-07-2A-FO\" /><Tag name=\"OS\" value=\"iOS\" /><Tag name=\"APP\" value=\"AGENTAPP\" /></Device></Agent><BillDetails><Biller id=\"VODA00000MUM03\" /><CustomerParams><Tag name=\"RefFld1\" value=\"\" /><Tag name=\"RefFld2\" value=\"\" /><Tag name=\"RefFld3\" value=\"\" /></CustomerParams></BillDetails></bbps:BillFetchRequest>",
"payloadType": "str",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 234,
"y": 400,
"wires": [
[
"87fc889c.4dc108",
"776a68c1.c29c38"
]
]
},
{
"id": "8ef14803.e62838",
"type": "debug",
"z": "9b34b2d0.13426",
"name": "Javascript Object from XML string",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"x": 980,
"y": 420,
"wires": []
},
{
"id": "7d78c49.0f8b33c",
"type": "debug",
"z": "9b34b2d0.13426",
"name": "Accessing value of a specific attribute of a tag ",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload[\"bbps:BillFetchRequest\"].Txn[0].$.msgId",
"targetType": "msg",
"x": 1020,
"y": 480,
"wires": []
},
{
"id": "75139548.fe6e3c",
"type": "debug",
"z": "9b34b2d0.13426",
"name": "JSon Object from XML String",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"x": 960,
"y": 340,
"wires": []
},
{
"id": "776a68c1.c29c38",
"type": "function-npm",
"z": "9b34b2d0.13426",
"name": "function-npm node for using fast-xml-parser",
"func": "var xmlData = msg.payload;\n\nvar parser = require('fast-xml-parser');\nvar he = require('he');\n\nvar options = {\n attributeNamePrefix : \"@_\",\n attrNodeName: \"attr\", //default is 'false'\n textNodeName : \"#text\",\n ignoreAttributes : false,\n ignoreNameSpace : false,\n allowBooleanAttributes : false,\n parseNodeValue : true,\n parseAttributeValue : true,\n trimValues: false,\n cdataTagName: \"__cdata\", //default is 'false'\n cdataPositionChar: \"\\\\c\",\n parseTrueNumberOnly: false,\n arrayMode: true, //\"strict\"\n attrValueProcessor: (val, attrName) => he.decode(val, {isAttributeValue: true}),//default is a=>a\n tagValueProcessor : (val, tagName) => he.decode(val), //default is a=>a\n stopNodes: [\"parse-me-as-string\"]\n};\n\n//if( parser.validate(xmlData) === true) { //optional (it'll return an object in case it's not valid)\n// var jsonObj = parser.parse(xmlData,options);\n//}\n\n// Intermediate obj\nvar tObj = parser.getTraversalObj(xmlData,options);\nvar jsonObj = parser.convertToJson(tObj,options);\n\nmsg.payload = jsonObj;\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 634,
"y": 340,
"wires": [
[
"75139548.fe6e3c"
]
]
}
]

XML to JSon parser using fast-xml-parser in function-npm node.

How-to:

Pass the XML string as payload to function-npm node. Assign the payload received at function-npm node to a variable. Pass the XML to the parse method with options (options can be changed as per requirement) Assign the return Json Object to msg.payload.

Note: function-npm is a function node for Node-RED with npm load capabilities.


Thanks to the team behind fast-xml-parser: https://github.com/NaturalIntelligence/fast-xml-parser

Thanks to the team behind the node function-npm: https://flows.nodered.org/node/node-red-contrib-function-npm

Any feedback on using this would be really appreciated.

Enjoy !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment