Modeling a JSON message for use in a message map by using an equivalent XML schema model
If you do not have a JSON schema for your JSON message data, you can use the following procedure to create an equivalent XML schema model that can be used in one or more message maps.
About this task
If you have a JSON schema for your message data, you can
use it as described in Creating or transforming a JSON message by using a JSON schema. If you
do not have a JSON schema, you can model a JSON message that contains
JSON objects, JSON arrays, or both, by following the steps in this
topic to create an equivalent XML schema model, which you can then
use in one or more message maps with the Cast
function
in the Graphical
Data Mapping editor. For more
information, see Graphically modeling a JSON message in a message map.
The following steps detail how to use an XML schema model in one or more message maps to define the JSON data from a corresponding XML schema type. Alternatively, you can define the JSON data in-line in the map by using the Add User-Defined function.
Procedure
Complete the following steps to create a JSON message based on a schema model:
Example
The following examples present XML schema types that model some example JSON data objects and arrays.
Example for a JSON object message
{ "Nam" : "A Name", "Num" : 1 }
(0x01000000:Object):JSON = ( ['json' : 0x2f030fa0]
(0x01000000:Object):Data = (
(0x03000000:NameValue):Nam = 'A Name' (CHARACTER)
(0x03000000:NameValue):Num = 1 (INTEGER)
)
)
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="MyJsonObjType1">
<xsd:sequence>
<xsd:element name="Nam" type="xsd:string" nillable="true"></xsd:element>
<xsd:element name="Num" type="xsd:int" nillable="true"></xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
The following figure shows the map after you cast the Data element of the JSON object message to the schema model MyJsonObjType1:
Example for a JSON array message
[ true,false ]
(0x01000000:Object):JSON = ( ['json' : 0x292051a0]
(0x01001000:Array):Data = (
(0x01000000:NameValue):Item = TRUE (BOOLEAN)
(0x01000000:NameValue):Item = FALSE (BOOLEAN)
)
)
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.ibm.com/iib/msl/json">
<xsd:complexType block="#all" name="JSONArray_ofBoolean">
<xsd:sequence>
<xsd:element maxOccurs="unbounded" minOccurs="0" name="Item" nillable="true" type="xsd:boolean"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
The following figure shows the map after you cast the Data element of the JSON object message to the schema model JSONArray_ofBoolean:
Example for JSON object message with an array of objects
[ { "nam":"one","val":1 },{ "nam":"two","val":2 }]
(0x01000000:Object):JSON = ( ['json' : 0x292051a0]
(0x01001000:Array):Data = (
(0x01000000:Object):Item = (
(0x03000000:NameValue):nam = 'one' (CHARACTER)
(0x03000000:NameValue):val = 1 (INTEGER)
)
(0x01000000:Object):Item = (
(0x03000000:NameValue):nam = 'two' (CHARACTER)
(0x03000000:NameValue):val = 2 (INTEGER)
)
)
)
You define a schema model for the JSON object message in the default namespace.
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.ibm.com/iib/msl/json">
<xsd:complexType block="#all" name="JSONArray_ofObjects">
<xsd:sequence>
<xsd:element maxOccurs="unbounded" minOccurs="0" name="Item" nillable="true">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="nam" type="xsd:string" nillable="true"/>
<xsd:element name="num" type="xsd:int" nillable="true"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
The following figure shows the map after you cast the Data element of the JSON object message to the schema model JSONArray_ofObjects:
Example for JSON Object message containing a JSON array
{ "state":"ok", "array":[ { "nam":"one","val":1 },{"nam":"two","val":2 } ] }
(0x01000000:Object):JSON = ( ['json' : 0x292051a0]
(0x01000000:Object):Data = (
(0x03000000:NameValue):state= 'ok' (CHARACTER)
(0x01001000:Array):array= (
(0x01000000:Object):Item = (
(0x03000000:NameValue):nam = 'one' (CHARACTER)
(0x03000000:NameValue):val = 1 (INTEGER)
)
(0x01000000:Object):Item = (
(0x03000000:NameValue):nam = 'two' (CHARACTER)
(0x03000000:NameValue):val = 2 (INTEGER)
)
)
)
)
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:mjatns="http://www.ibm.com/iib/msl/json">
<xsd:import namespace="http://www.ibm.com/iib/msl/json" schemaLocation="MyJsonArrayTypes.xsd"/>
<xsd:complexType name="MyJsonObjWithArrayType1">
<xsd:sequence>
<xsd:element name="state" type="xsd:string" nillable="true"></xsd:element>
<xsd:element name="array" type="mjatns:JSONArray_ofObjects" nillable="true"></xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.ibm.com/iib/msl/json">
<xsd:complexType block="#all" name="JSONArray_ofObjects">
<xsd:sequence>
<xsd:element maxOccurs="unbounded" minOccurs="0" name="Item" nillable="true">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="nam" type="xsd:string" nillable="true"/>
<xsd:element name="num" type="xsd:int" nillable="true"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
The following figure shows the map after you cast the Data element of the JSON object message to the schema model MyJsonObjWithArrayType1:
<xsd:element name="Data" type="MyJsonObjType1"></xsd:element>
What to do next
Deploy the message map and verify that the output message is valid. For more information, see Troubleshooting a message map.