WebSphere Message Broker, Version 8.0.0.7
Operating Systems: AIX, HP-Itanium, Linux, Solaris, Windows, z/OS
See information about the latest product version
See information about the latest product version
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
- PHP as output_assembly->JSON->Data->path to required object or array
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 broker 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 PHP as:
$output_assembly->JSON->Data->address->age = $input_assembly->JSON->Data->address->age + 22; // Set age to 21
$output_assembly->JSON->Data->belongings[2] = 'an other';
$output_assembly->JSON->Data->belongings[3] = 'and another';
For
more information about PHP arrays, including multidimensional arrays,
see Using PHP arrays with JSON.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 broker 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 PHP as:
$output_assembly->JSON->Data.orders[0][0] // “thing1”
$output_assembly->JSON->Data.orders[1][2] // 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'