You can create JSON message data that contains JSON objects, JSON arrays, or both, by creating elements in the logical message tree, under the Data element that is owned by the JSON parser root.
A JSON message can have either an anonymous object or an anonymous array as the root of the data. When you create a JSON array in the logical message tree, the JSON array name is placed in a tree element that has a type that is set to the JSON parser element type JSON.Array.
The items in the JSON array are placed in the logical tree as NameValue elements, as children of the JSON.Array element, with the required value. The names of these array item elements are not used by the JSON serializer because JSON array items are anonymous. However, for consistency with the name that is used by the JSON parser, use the name Item when you define the array item elements.
The following example shows how to create a JSON message that is formatted with an object at the root level, with the form { --- }.
{"Message":"Hello World"}
SET OutputRoot.JSON.Data.Message = 'Hello World';
MbElement outRoot =
outMessage.getRootElement();
MbElement outJsonRoot =
outRoot.createElementAsLastChild(MbJSON.PARSER_NAME);
MbElement outJsonData =
outJsonRoot.createElementAsLastChild(MbElement.TYPE_NAME, MbJSON.DATA_ELEMENT_NAME, null);
MbElement outJsonTest =
outJsonData.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Message", "Hello World");
$output_message->{MB_JSON_PARSER_NAME}->{MB_JSON_DATA_ELEMENT_NAME}->Message = 'Hello World';
Message: ( ['json' : 0xc552990]
(0x01000000:Object ):Data = (
(0x03000000:NameValue):Message = 'Hello World' (CHARACTER)
)
The following example shows how to create a message that is formatted with an array at the root level, in the form [ --- ].
["valueA","valueB"]
CREATE FIELD OutputRoot.JSON.Data IDENTITY (JSON.Array)Data;
CREATE LASTCHILD OF OutputRoot.JSON.Data TYPE NameValue NAME 'Item' VALUE 'valueA';
CREATE LASTCHILD OF OutputRoot.JSON.Data TYPE NameValue NAME 'Item' VALUE 'valueB';
MbElement outJsonRoot =
outRoot.createElementAsLastChild("JSON");
MbElement outJsonData =
outJsonRoot.createElementAsLastChild(MbJSON.ARRAY, "Data", null);
outJsonData.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,
"Item", "valueA");
outJsonData.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,
"Item", "valueB");
$output_assembly->JSON->Data[] = array("valueA","valueB");
For more information about PHP arrays, see Using PHP arrays with JSON.
Message: ( ['json' : 0xc552990]
(0x01001000:Array ):Data = (
(0x03000000:NameValue):Item = 'valueA' (CHARACTER)
(0x03000000:NameValue):Item = 'valueB' (CHARACTER)
)
The following example shows how to create a message that is formatted with an array of objects at the root level, in the form [{--},{--},...].
[{"Nam1":"val1","Num1":1},{"Nam2":"val2","Num2":2}]
CREATE FIELD OutputRoot.JSON.Data IDENTITY(JSON.Array)Data;
SET OutputRoot.JSON.Data.Item[1].Nam1 = 'val1';
SET OutputRoot.JSON.Data.Item[1].Num1 = 1;
SET OutputRoot.JSON.Data.Item[2].Nam2 = 'val2';
SET OutputRoot.JSON.Data.Item[2].Num2 = 2;
MbElement jsonData =
outMessage.getRootElement().createElementAsLastChild(MbJSON.PARSER_NAME).createElementAsLastChild
(MbJSON.ARRAY,MbJSON.DATA_ELEMENT_NAME, null);
MbElement jsonFirstArrayItem =
jsonData.createElementAsLastChild(MbElement.TYPE_NAME, MbJSON.ARRAY_ITEM_NAME, null);
jsonFirstArrayItem.createElementAsFirstChild(MbElement.TYPE_NAME_VALUE, "Nam1", "val1");
jsonFirstArrayItem.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Num1", new Integer(1));
MbElement jsonSecondArrayItem =
jsonData.createElementAsLastChild(MbElement.TYPE_NAME, MbJSON.ARRAY_ITEM_NAME, null);
jsonSecondArrayItem.createElementAsFirstChild(MbElement.TYPE_NAME_VALUE, "Nam2", "val2");
jsonSecondArrayItem.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Num2", new Integer(2));
Message: ( ['json' : 0xc673900]
(0x01001000:Array):Data = (
(0x01000000:Object):Item = (
(0x03000000:NameValue):nam1 = 'val1' (CHARACTER)
(0x03000000:NameValue):num1 = 1 (INTEGER)
)(0x01000000:Object):Item = (
(0x03000000:NameValue):nam2 = 'val2' (CHARACTER)
(0x03000000:NameValue):num2 = 2 (INTEGER)
)
)
)