Adding a Processing Rule to convert XML to JSON
In this section you will add a Processing Rule which allows a client to POST XML to the service with the URI /tojson to be able to convert XML-to-JSON or XML to PlainJSON.
- Create a sub-directory on the local:/// directory to organize the artifacts for the Processing Policy by navigating to the File Management, then click on the Action > Create Subdirectory.
- Create the directory Utility_XMLF and click Confirm Create.
- Navigate back into the Utility_XMLF service and click on Edit button for the Processing Policy “Utility_XMLF”.
- Click on New Rule and update the name to Utility_XMLF_rule_xml2json.
- Configure the Match Action:
- Double click on the Match Action to open it.
- Click on Add New icon to add a new matching rule.
- Name it tojson
- Click on Add to add a new Matching Type.
- Enter the following URL match: */tojson
- Click Apply and continue to apply until you get back to the Utility_XMLF Policy.
- Locate the GatewayScript Action and drag it after the Match Action.
- Double-click to open the GatewayScript Action and click on Upload… to upload the toJson.js script. Click Done afterwards.
- Back in the Configure XML Firewall Style Policy, move the Utility_XMLF_rule_xml2json rule up to the top. Click Apply Policy afterwards.
Click Close Window to close the Configure XML Firewall Style Policy window.
- Back in the Configure XML Firewall window, find the Request Type, drop it down, and select Non-XML. Click Apply and Save Config.
- To test the service, send the following cURL command to the /tojson endpoint with the following command:
curl –data-binary “<xml>hello</xml>” http://<your_dataPower_URL>:2088/tojson
Summary: In this section you have learned how to add a Processing Policy, with another URI identifier on the same loopback echo service, where a client may invoke to POST and convert an XML body.
The echo endpoind still exist if you invoke http://<your_datapower_url>:2088, but now, by passing the /tojson uri, you have a XML-to-JSON function on the same XML Firewall service.
Note (Badgerfish): You’ll notice that the response will contain a “$” in the json (e.g. {“xml”:{“$”:”hello”}}). This is because the gatewayscript converter.toJSON() uses a conversation standard called Badgerfish. The format would account for attributes and namespace elements in XML nodes, if any.
More details about badgerfish may be found here: http://badgerfish.ning.com/
Details for the converter.toJSON DataPower function may be found here: https://www.ibm.com/support/knowledgecenter/SS9H2Y_7.7.0/com.ibm.dp.doc/json-xml-converter_js.html#converter.toJSON
Optional
You may create another Processing Policy to accept Plain JSON (strips badgerfish from the converter.toXML datapower function) or import the toPlainJson.js script to step #7.
Be wary, the toPlainJson.js will not account for different namesapces and attributes.
For example, if you have the following:
<element name=”xyz”>hello</element>
The output from the toPlainJson (striped badgerfish) will output:
{
“element”: “hello”
}
The output from the toJson.js (badgerfish) will output:
{
“element”: {
“@name”: “xyz”,
“$”: “hello”
}
}
You may use the same steps as shown in the this section, but here are the shortened instructions to create the new Processing Policy for a XML-to-Plain JSON Processing Rule:
- Navigate back into the Utility_XMLF is not already there, and edit the Processing Policy: Utility_XMLF.
- When the Configure XML Firewall Style Policy pops up, click on New RUle, and update the name to Utility_XMLF_rule_xml2plainjson.
- Double-click on the Match Action and create a math rule titled toPlainJson with a URL match */toplainjson.
- Drag and drop a GatewayScript Action and upload the toPlainJson.js into the Utility_XMLF directory.
- Apply the policy, save the XML Firewall, and Save Configuration.
- Test.
You will now notice by invoking http://<your_datapower_url>:2088/toplainjson you will not get badgerfish any more.