Modificación de un mensaje JSON
Puede modificar los objetos JSON y las matrices JSON.
Las corrientes de datos JSON se analizan en el árbol de mensajes lógico y se colocan bajo el
Data
que es propiedad de laJSON
raíz de analizador. Es posible acceder y modificar los objetos de datos JSON y matrices desde cada lenguaje soportado de la manera siguiente:- ESQL como
OutputRoot.JSON.Data.path to required object or array
- Java™ como
/JSON/Data/path to required object or array
También puede manipular mensajes JSON en Editor de correlaciones de datos gráficas. Para obtener más información, consulte Creación o transformación de un mensaje de salida JSON utilizando una correlación de mensajes.
El ejemplo siguiente muestra un mensaje JSON posible:
{
"name" : "John Doe",
"age" : -1,
"known" : false,
"address" : { "street" : null,
"city" : "unknown" },
"belongings" : ["this", "that", "the other"]
}
El analizador JSON analiza la corriente de bits JSON de entrada para generar el siguiente árbol lógico de mensajes del nodo de integración:
(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)
)
)
)
Este árbol de mensajes puede modificarse utilizando ESQL como:
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';
El árbol de mensajes se puede modificar a través de Java como:
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 con una matriz multidimensional
El ejemplo siguiente muestra la entrada JSON que contiene una matriz multidimensional:
{
"customer" : "Joe",
"orders" : [ [ "thing1", 1, 10.1 ],
[ "thing2", 2, 20.2 ] ]
}
Se genera el siguiente árbol de mensajes del nodo de integración:
(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)
)
)
)
)
Puede acceder a este árbol de mensajes
a través de ESQL de la siguiente manera (puede utilizar el
nombre Item o un asterisco (*) como comodín):
InputRoot.JSON.Data.orders.Item[1].Item[1] -- 'thing1'
InputRoot.JSON.Data.orders.*[2].*[3] -- 2.02E+1
El árbol de mensajes se accede a través de Java de la siguiente manera (puede utilizar el nombre Artículo, que el analizador JSON da a los elementos de matriz, o un asterisco (*) como comodín):
inMessage.getRootElement().getFirstElementByPath("/JSON/Data/orders/Item[1]/Item[1]"); // 'thing1'
inMessage.getRootElement().getFirstElementByPath("/JSON/Data/orders/*[2]/*[3]"); // '2.02'