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

The Content 20.16.02 and later releases include a utility to convert JSON data to XML data. You can use the utility instead of the instructions on this page. See XML utilities for more information about the utilities.

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.

Simple JSON sample
 {
  "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.

XML conversion
 <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 ( &gt; ).

The following JSON sample

JSON with angle brackets
 {
  "username" : "Joe",
 "password" : "password!>"
}

is converted to this XML.

XML conversion with angle brackets escaped
<json>
  <username>Joe</username>
  <password>password!&gt;</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 prefixed with an underscore ( _ ).

The following JSON sample

JSON keys starting with numbers
{
  "1username" : "Joe",
 "2password" : "password!>"
}

is converted to this XML.

XML conversion with underscores preceding numbered keys
<json>
  <_username>Joe</_username>
  <_password>password!&gt;</_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

JSON keys with white spaces
{
  "Name": "Name", 
  "Version": "Version", 
  "Software    Manufacturer":  "Software Manufacturer", 
  "Type": "Type", 
  "Installation Directory": "Installation Directory"
}

is converted to this XML.

XML conversion with hyphens replacing white spaces
<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 ( ).

JSON keys with white spaces
{
  "User@Name": "Name", 
  "1Version@": "Version", 
  "Software    Manufacturer":  "Software Manufacturer", 
  "Type": "Type", 
  "Installation Directory": "Installation Directory"
}

is converted to this XML.

XML conversion with hyphens replacing white spaces
<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

  1. In  Development Studio's Designer, open the process that contains the workflow that you want to configure.
  2. Double-click the appropriate activity (typically an Assign activity) to open the Property panel.
  3. Add a new assignment or edit an existing assignment.
  4. Choose the Advanced XSLT transform editor.
  5. In the wizard, select a sample source and click Finish to view the XSLT transform editor.
  6. In the XSLT transform editor's XSLT Stylesheet, select the XSLT Stylesheet node and click Additional Namespaces.
  7. Click the Add a namespace entry icon  and add a JSON namespace (prefix and URI) as shown in the following image.
  8. Click OK and then Apply in the Stylesheet.
  9. Write a transform that invokes the JSON to XML conversion method, as shown in the following example. 

    1. Add a root element named XML.

    2. 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. 

    3. If the input is not a JSON string, then add the exact JSON string to the resulting XML.
  10. Click Save and Exit the Stylesheet.
  11. Add another XSLT transform to convert the XML string generated by the previous transform to an XML data type, as shown in the example.
    1. Add a root element named XML.
    2. Add the XML string created in the previous transform as a token under the root element named XML.
  12. Click Save and Exit the Stylesheet.
Was this page helpful? Yes No Submitting... Thank you

Comments