Creating a JSON message

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. It is not possible to put a JSON string literal, number, boolean, or null value as the root of the data using the JSON parser. 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.

Creating a JSON object message

The following example shows how to create a JSON message that is formatted with an object at the root level, with the form { --- }.

This example shows how to create a simple JSON object message with one name-value pair:
{"Message":"Hello World"}
The following ESQL code can be used to create the message:
SET OutputRoot.JSON.Data.Message = 'Hello World';
The following Java™ code can also be used to create the message:
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");

Creating a JSON array message

The following example shows how to create a message that is formatted with an array at the root level, in the form [ --- ].

This example shows how to create a JSON array message:
["valueA","valueB"]
The following ESQL code can also be used to create the array:
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';
The following Java code can also be used to create the array:
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");
The message is created in the integration node logical message tree in the following form:
Message: ( ['json' : 0xc552990]
  (0x01001000:Array     ):Data    = (
    (0x03000000:NameValue):Item = 'valueA' (CHARACTER)
    (0x03000000:NameValue):Item = 'valueB' (CHARACTER)
 )

Creating a JSON object array message

The following example shows how to create a message that is formatted with an array of objects at the root level, in the form [{--},{--},...].

This example shows how to create the JSON object array message:
[{"Nam1":"val1","Num1":1},{"Nam2":"val2","Num2":2}]
The following ESQL code can also be used to create the array:
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;
The following Java code can also be used to create the array:
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));
The message is created in the integration node logical message tree in the following form:
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)
    )
  )
)