IBM Integration Bus, Version 9.0.0.8 Operating Systems: AIX, HP-Itanium, Linux, Solaris, Windows, z/OS

See information about the latest product version

JSON conventions for calling an integration service by using a JavaScript client API

It is possible for data to be lost when messages are automatically converted from JSON (JavaScript Object Notation) to XML, or from XML to JSON. However, you can minimize the potential for data loss during message transformation if you use documented conventions to declare and identify specific XML information within a JSON data structure definition.

When you use the JavaScript client API to call an integration service, the messages that are sent from the JavaScript client API must be converted from JSON to XML to be processed by the integration service. Messages from the integration service must be converted from XML to JSON to be retrieved by the JavaScript client API.

Since JSON has no equivalent concept for an XML attribute, conventions are used within JSON data structure definitions to distinguish between element values and attributes, and to specify how a JSON field name and associated value should be represented as an XML attribute and associated value.

The following conventions are used in IBM® Integration Bus to handle attributes and element values:
Convention Example
If an element has a value but no attributes, you can use standard JSON notation to indicate the value of the element. {"circle":"Red Circle"}

is equivalent to the XML string

<circle>Red Circle</circle>
If an element has attributes, you must add @ to the beginning of a JSON object name to indicate that the object is an attribute. {"circle":{"@color":"red"}}

is equivalent to the XML string

<circle color="red"/>
If an element has both an attribute and a value, you must use the notation #text to indicate that an object is an element value. {"circle":{"@color":"red","#text":"Red Circle"}}

is equivalent to the XML string

<circle color="red">Red Circle</circle>
If an element has a value but no attributes, you can use standard JSON notation to retrieve the element value.
var myVar = {"circle":"Red Circle"};
var circleValue = myVar.circle
The second line retrieves the value of the element "circle" from inside the JSON variable "myVar".
If an element has attributes, you must retrieve an attribute value by surrounding the attribute name with double quotation marks and square brackets.
var myVar = {"circle":{"@color":"red"}};
var circleColor = myVar.circle["@color"]
The second line retrieves the value of the attribute "color" from the element "circle" inside the JSON variable "myVar".
If an element has both an attribute and a value, you must retrieve the element value by using the notation ["#text"].
var myVar = {"circle":{"@color":"red","#text":"Red Circle"}};
var circleValue = myVar.circle["#text"]
The second line retrieves the value of the element "circle" from inside the JSON variable "myVar".

Limitations

There is no support for conversion of the following constructs between JSON and XML:
An XML element that contains mixed content
An output message that returns an XML element that contains an element value and nested elements cannot be converted to JSON. For example, the following XML message cannot be converted to JSON:
<shapes dimensions="2D">My Shapes
   <circle color="red">Red Circle</circle>
   <triangle color="yellow">Yellow Triangle</triangle>
</shapes>
  
However, the following XML message can be converted:
<shapes dimensions="2D">
   <circle color="red">Red Circle</circle>
   <triangle color="yellow">Yellow Triangle</triangle>
</shapes>
  
The JSON _type property
An input message that contains the JSON _type property cannot be converted to XML. For example, the following JSON message cannot be converted to XML:
myCircle:{"__type":"Circle:http://www.example.org/primitiveTypesTest/",
   "x":2 ,
   "y":3 , 
   "radius":10
} 

ss26025_.htm | Last updated Friday, 21 July 2017