How to keep square brackets when using REST JSON Plugin to parse and render one element JSON Array

How to keep square brackets when using REST JSON Plugin to parse and render one element JSON Array

book

Article ID: KB0083971

calendar_today

Updated On:

Products Versions
TIBCO ActiveMatrix BusinessWorks Plug-in for REST and JSON 2.0.0

Description

Below is an example for a JSON Array. In the JSON Array "cars" there are three elements:
========
{
"name":"John",
"age":30,
"cars":[ "Ford", "BMW", "Fiat" ]
}
========
 
If we use the REST JSON plugin Parse JSON activity to parse the above JSON to XML and then use Render JSON activity to render the XML string, the output of Parse JSON activity is like the following:

=======
<JSON>
   <name>John</name>
   <age>30</age>
   <cars>Ford</cars>
   <cars>BMW</cars>
</JSON>
=======  

The output for Render JSON activity will be the same as the initial JSON string:

========
{
"name":"John",
"age":30,
"cars":[ "Ford", "BMW" ]
}
========

However, sometimes the customer's JSON array only contains one element. For example 

========
{
"name":"John",
"age":30,
"cars":[ "Ford" ]
}
========

In this case the output of Parse JSON activity will be: 

=======
<JSON>
   <name>John</name>
   <age>30</age>
   <cars>Ford</cars>
</JSON>
=======  

and the output of the Render JSON activity will be:

========
{
"name":"John",
"age":30,
"cars":"Ford"
}
========

You can see that the square brackets is missing.

Issue/Introduction

How to keep square brackets when using REST JSON plugin to parse and render one element JSON Array.

Environment

Operation Systems: All

Resolution

This issue is due to the functionalities we use for XML to JSON conversion or vise versa and are based on generalized rules. The significance of JSON Array is to handle an element with occurrence more than 1 as a single occurrence already handled by JSON Object (simple name-value pair). The workaround for this is to add a "null" value in the JSON string after the first array element. In this case, the square brackets will be kept in the output.

Input JSON:

========
{
"name":"John",
"age":30,
"cars":[ "Ford", null]
}
========

Parse JSON activity XML output:

========
<JSON>
   <name>John</name>
   <age>30</age>
   <cars>Ford</cars>
   <cars/>
</JSON>
========

Render JSON activity JSON output:

========
{
"name":"John",
"age":30,
"cars":[ "Ford", ""]
}
========