In the Graphical Data Mapping editor, you can use the Add User-Defined function to transform heterogeneous JSON data.
Create a message map with the output domain set to JSON. For more information, see Graphically modeling a JSON message in a message map
The approach for modeling JSON heterogeneous data is to define each structure as a union of the different data that can be present in each possible form. Then, set the data, which is present in some instances of the data, as optional.
By default, when you model JSON data in the Graphical Data Mapping editor, you create objects and arrays where all instances are the same. All objects with the same name have the same set of data fields, and all entities in an array have the same set of data fields. This use case matches the most common usage of JSON data formats.
JSON allows entries in JSON arrays to have different structure and types. For JSON Objects, some JSON applications may also allow the same name for contained objects or values which may then have different structure and types. The JSON specification states: "The names within an object SHOULD be unique." For more information, see The JavaScript Object Notation (JSON) Data Interchange Format). Only use this format if it is required by the external applications. This format of JSON data is termed heterogeneous JSON data.
If you have a JSON schema for your message data, you can use that as described in Creating or transforming a JSON message by using a JSON schema. Alternatively, you can model heterogeneous JSON data by using the Add User-Defined function as described in this topic, or you can use an equivalent XML schema model as described in Modeling a JSON message for use in a message map by using an equivalent XML schema model.
Complete the following steps to model heterogeneous JSON data in the Graphical Data Mapping editor:
This example shows how to transform a JSON object message that describes a vehicle that uses two heterogeneous JSON data objects with different named values into a heterogeneous JSON array message where each entry is a data object with different named values.
The input application produces a heterogeneous JSON object message that describes a vehicle that uses two heterogeneous JSON data objects. Each JSON data object is called attribute and contains different name-value elements. The input JSON Data is as below:
{ "Description" : {
"attribute" : {
"colour" : "blue",
"metallic" : true
},
"attribute" : {
"capacity" : 1.4,
"fuel" : "diesel"
}
}
}
[{"attributeType":"Paint","colour":"blue","metallic":true},
{"attributeType":"Engine","capacity":1.4E+0,"fuel":"diesel"}]
To transform from the input to the required output, you use the For Each transform to create each of the entries in the array from the same named (repeating) attribute input objects.
The nested map that is associated to the If transform sets the attributeType element to Paint and copies the value of the other elements unchanged.
The other nested map that is associated to the Else sets the attributeType element to Engine and copies the value of the other elements unchanged.
[{"attributeType":"Paint","colour":"blue","metallic":true},
{"attributeType":"Engine","capacity":1.4E+0,"fuel":"diesel"}]
(0x01000000:Object):JSON = ( ['json' : 0x298f1e00]
(0x01001000:Array):Data = (
(0x01000000:Object):Item = (
(0x03000000:NameValue):attributeType = 'Paint' (CHARACTER)
(0x03000000:NameValue):colour = 'blue' (CHARACTER)
(0x03000000:NameValue):metallic = TRUE (BOOLEAN)
)
(0x01000000:Object):Item = (
(0x03000000:NameValue):attributeType = 'Engine' (CHARACTER)
(0x03000000:NameValue):capacity = 1.4E+0 (FLOAT)
(0x03000000:NameValue):fuel = 'diesel' (CHARACTER)
)
)
)
Deploy the message map and verify that the output message is valid. For more information, see Troubleshooting a message map.