Configuring a workflow to convert JSON data to XML data
You might need to convert JSON data to XML format for workflow interaction with some third party REST services. This topic describes how to configure a workflow to convert JSON data to XML, so that you can perform additional required XSLT and XPATH operations. You can also convert XML data to JSON before sending the request to a REST service.
You can use the XSLT transform editor in Development Studio to configure existing or new workflows.
Note
This feature is supported in BMC Atrium Orchestrator Platform version 7.8.02 or later.
JSON to XML data conversion formatting changes
Workflows that you set up to convert JSON data to XML data will convert the JSON data so that it meets XML formatting requirements. Specifically, JSON keys do not have the XML node naming restrictions. This section provides some examples of how the key values are converted to conform to XML standards. For details about XML node naming conventions, see the standards documentation.
JSON conversion example
In the following simple JSON data sample username and password are names of keys and Joe and password! are values assigned to those keys.
{
"username" : "Joe",
"password" : "password!"
}
XML requires a root node and JSON does not have a concept of root node, so the converted XML includes a default root node named <json>.
The previous JSON sample is converted to the following XML.
<json>
<username>Joe</username>
<password>password!</password>
</json>
Escaping characters example
Some characters are reserved in XML (for example, angle brackets). When JSON data is converted to XML those characters are escaped. For example, a right angle bracket ( > ) is replaced with ( > ).
The following JSON sample
{
"username" : "Joe",
"password" : "password!>"
}
is converted to this XML.
<json>
<username>Joe</username>
<password>password!></password>
</json>
Keys starting with numbers example
XML node names cannot start with numerical characters. When converting from JSON to XML, keys that start with numbers are replaced an underscore ( _ ).
The following JSON sample
{
"1username" : "Joe",
"2password" : "password!>"
}
is converted to this XML.
<json>
<_username>Joe</_username>
<_password>password!></_password>
</json>
Keys with white spaces example
XML node names cannot contain white spaces. When converting from JSON to XML, one or more white spaces in a key are replaced with a hyphen ( - ).
The following JSON sample
{
"Name": "Name",
"Version": "Version",
"Software Manufacturer": "Software Manufacturer",
"Type": "Type",
"Installation Directory": "Installation Directory"
}
is converted to this XML.
<json>
<Name>Name</Name>
<Version>Version</Version>
<Software-Manufacturer>Software Manufacturer</Software-Manufacturer>
<Type>Type</Type>
<Installation-Directory>Installation Directory</Installation-Directory>
</json>
Keys with invalid XML characters example
XML node names cannot contain certain characters, such as @ or 1. When converting from JSON to XML, if these invalid XML characters occur in the beginning, they are replaced by an underscore; elsewhere, they are replaced with a hyphen ( - ).
In following JSON sample, @ character in the key User@Name and Version@ is replaced by a hyphen ( - ) and the first number 1 in the key 1Version@ is replaced by an underscore ( _ ).
{
"User@Name": "Name",
"1Version@": "Version",
"Software Manufacturer": "Software Manufacturer",
"Type": "Type",
"Installation Directory": "Installation Directory"
}
is converted to this XML.
<json>
<User-Name>Name</User-Name>
<_Version->Version</_Version->
<Software-Manufacturer>Software Manufacturer</Software-Manufacturer>
<Type>Type</Type>
<Installation-Directory>Installation Directory</Installation-Directory>
</json>
Configuring a workflow that converts JSON to XML
Complete the steps in this section to configure a workflow to use the JSON library to convert JSON to XML.
For more information about working with XSLT transform editors and creating namespaces, see XSLT transform editor and Additional namespaces.
- In Development Studio's Designer, open the process that contains the workflow that you want to configure.
- Double-click the appropriate activity (typically an Assign activity) to open the Property panel.
- Add a new assignment or edit an existing assignment.
- Choose the Advanced XSLT transform editor.
- In the wizard, select a sample source and click Finish to view the XSLT transform editor.
- In the XSLT transform editor's XSLT Stylesheet, select the XSLT Stylesheet node and click Additional Namespaces.
- Click the Add a namespace entry icon and add a JSON namespace (prefix and URI) as shown in the following image.
- Click OK and then Apply in the Stylesheet.
- Write a transform that invokes the JSON to XML conversion method, as shown in the following example.
Use the following steps to write the transform.Add a root element named XML.
Invoke the fromJSONString method as json:XML.fromJSONString(., "json").
This invokes a method named fromJSONString(..) in a class named XML, passing it the value set in the input context item.
The second argument "json" to the method fromJSONString(..) sets the root node name to (json) in the resulting XML string.Note
Make sure that the JSON string represented by the input context item is valid.
If the input is not a JSON string, then add the exact JSON string to the resulting XML.
- Click Save and Exit the Stylesheet.
- Add another XSLT transform to convert the XML string generated by the previous transform to an XML data type, as shown in the example.
- Add a root element named XML.
- Add the XML string created in the previous transform as a token under the root element named XML.
- Click Save and Exit the Stylesheet.
Comments
Log in or register to comment.