In the Graphical Data Mapping editor, you can use the Cast function to create a JSON message that contains JSON objects, JSON arrays, or both. You must provide your own schema message model.
Complete the following steps to create a JSON message based on a schema model:
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:
Deploy the message map and verify that the output message is valid. For more information, see Troubleshooting a message map.