Modifying a JSON message

You can modify JSON objects and JSON arrays.

JSON data streams are parsed into the logical message tree and placed under the Data element that is owned by the JSON parser root. The JSON data objects and arrays can be accessed and modified from each supported language as follows:
  • ESQL as OutputRoot.JSON.Data.path to required object or array
  • Java™ as /JSON/Data/path to required object or array

You can also manipulate JSON messages in the Graphical Data Mapping editor. For more information, see Creating or transforming a JSON output message by using a message map.

The following example shows a possible JSON message:
{
    "name" : "John Doe",
    "age" : -1,
    "known" : false,
    "address" : { "street" : null,
                  "city" : "unknown" },
    "belongings" : ["this", "that", "the other"]
}
The JSON parser parses the input JSON bit stream, to produce the following integration node logical message tree:
(Message):JSON     = ( ['json' : 0xhhhhhh]
    (0x01000000:Object):Data      = ( 
      (0x03000000:NameValue): name    = 'John Doe' (CHARACTER)
      (0x03000000:NameValue): age     = -1 (INTEGER)
      (0x03000000:NameValue): known   = FALSE (BOOLEAN)
      (0x01000000:Object     ): address = (
        (0x03000000:NameValue): street = NULL (UNKNOWN)
        (0x03000000:NameValue): city = 'unknown' (CHARACTER)
      )
      (0x01001000:Array     ): belongings = (  
        (0x03000000:NameValue): Item = 'this' (CHARACTER)
        (0x03000000:NameValue): Item = 'that' (CHARACTER)
        (0x03000000:NameValue): Item = 'the other' (CHARACTER)
      )
    )
  )
This message tree can be modified through ESQL as:
SET OutputRoot.JSON.Data.age =  InputRoot.JSON.Data.age + 22;   -- Set age to 21
SET OutputRoot.JSON.Data.belongings.Item[4] = 'an other';
SET OutputRoot.JSON.Data.belongings.Item[5] = 'and another';
The message tree can be modified through Java as:
MbElement ageEl = message.getRootElement().getLastChild().getFirstElementByPath("/JSON/Data/age");
int age = ((Integer)ageEl.getValue()).intValue();
ageEl.setValue(age + 22);       // Set age to 21
inMessage.getRootElement().getLastChild().getFirstElementByPath("/JSON/Data/belongings/Item[3]").setValue('an other');

JSON with a multidimensional array

The following example shows JSON input containing a multidimensional array:
{
    "customer" : "Joe",
    "orders" : [ [ "thing1", 1, 10.1 ],
                 [ "thing2", 2, 20.2 ]  ]
}
The following integration node message tree is produced:
(Message):JSON   = ( ['json' : 0xhhhhhh] 
    (0x01000000:Object):Data   = (
      (0x03000000:NameValue):customer = 'Joe' (CHARACTER)
      (0x01001000:Array):orders = (  
        (0x01001000:Array):Item   = (  
          (0x03000000:NameValue):Item = 'thing1' (CHARACTER)
          (0x03000000:NameValue):Item = 1 (INTEGER)
          (0x03000000:NameValue):Item = 1.01E+1 (FLOAT)
        ) 
        (0x01001000:Array):Item   = (  
          (0x03000000:NameValue):Item = 'thing2' (CHARACTER)
          (0x03000000:NameValue):Item = 2 (INTEGER)
          (0x03000000:NameValue):Item = 2.02E+1 (FLOAT)
        ) 
      ) 
    ) 
  )
This message tree is accessed through ESQL in the following way (you can use either the name Item or an asterisk (*) as a wildcard):
InputRoot.JSON.Data.orders.Item[1].Item[1]   -- 'thing1'
InputRoot.JSON.Data.orders.*[2].*[3]         -- 2.02E+1
The message tree is accessed through Java in the following way (you can use either the name Item, which the JSON parser gives to array items, or an asterisk (*) as a wildcard):
inMessage.getRootElement().getFirstElementByPath("/JSON/Data/orders/Item[1]/Item[1]");  // 'thing1'
inMessage.getRootElement().getFirstElementByPath("/JSON/Data/orders/*[2]/*[3]");  // '2.02'