Author |
Message
|
archana123 |
Posted: Thu Aug 20, 2015 5:48 am Post subject: Unable to modify the JSON data in java compute node |
|
|
Novice
Joined: 21 Jul 2015 Posts: 14
|
I am a newbie. I am having an HTTP Restful web service flow. I accept JSON as input. I wanted to manipulate few fields in it and propagate it forward.
Flow: HTTPInput ---> JCN ---> MQ
Input can be :
Code: |
(0x01000000:Object):JSON = ( ['json' : 0x7f60780626a0]
(0x01000000:Object):Data = (
(0x03000000:NameValue):Notification_Version = '2.2' (CHARACTER)
(0x03000000:NameValue):Message_Type = 'T est' (0x01001000:Array ):SDT = (
(0x01000000:Object):Item = (
(0x03000000:NameValue):SDateTimestamp = '15-Jun-2015 18:39:04.48' (CHARACTER)
(0x01001000:Array ):Parameter = (
(0x01000000:Object):Item = (
(0x03000000:NameValue):Name = Param' (CHARACTER)
(0x03000000:NameValue):Value = '47' (CHARACTER)
)
(0x01000000:Object):Item = (
(0x03000000:NameValue):Name = 'Speed' (CHARACTER)
(0x03000000:NameValue):Value = '1344' (CHARACTER)
)
(0x01000000:Object):Item = (
(0x03000000:NameValue):Name = 'Hours' (CHARACTER)
(0x03000000:NameValue):Value = '26182.0' (CHARACTER)
)
... .... |
etc etc
I wanted to modify the data
Code: |
MbElement outBody = outMessage.getRootElement().getFirstElementByPath("/JSON/Data/Message_Type");
String jsonMsg = outBody.getValueAsString();
String refineData = jsonMsg.replace(" +", " ");
|
But when I try to modify any value, I am getting an exception. Please see below:
Code: |
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode>SOAP-ENV:Server</faultcode>
<faultstring>BIP3113E: Exception detected in message flow HTTPInputToMQ (integration node DevIntegration) </faultstring>
<faultactor>http://localhost:7080/HTTPInputMessageFlow</faultactor>
<detail><text>Exception. BIP2230E: Error detected whilst processing a message in node 'HTTPInputToMQ.Delete HTTP Hdr'. : /build/slot1/S000_P/src/DataFlowEngine/PluginInterface/ImbJniNode.cpp: 1273: ImbJniNode::evaluate: ComIbmHTTPHeaderNode: HTTPInputToMQ#FCMComposite_1_7
BIP2230E: Error detected whilst processing a message in node 'HTTPInputToMQ.Process Message'. : /build/slot1/S000_P/src/DataFlowEngine/PluginInterface/ImbJniNode.cpp: 1273: ImbJniNode::evaluate: ComIbmJavaComputeNode: HTTPInputToMQ#FCMComposite_1_6
BIP4367E: The method ''evaluate'' in Java node ''Process Message'' has thrown the following exception: 'java.lang.NullPointerException'. : /build/slot1/S000_P/src/DataFlowEngine/PluginInterface/com_ibm_broker_plugin_CMbService.cpp: 1938: ImbJavaExceptionUtils::throwableToNativeException: :
BIP4395E: Java exception: ''java.lang.NullPointerException''; thrown from class name: ''HTTPInputToMQ_ProcMsg'', method name: ''evaluate'', file: ''HTTPInputToMQ_ProcMsg.java'', line: '64' : /build/slot1/S000_P/src/DataFlowEngine/PluginInterface/com_ibm_broker_plugin_CMbService.cpp: 1956: ImbJavaExceptionUtils::throwableToNativeException: : </text></detail>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
|
I actually need to modify the array values. But initially tried with a simple key-value.
Please help me with it.
Last edited by archana123 on Thu Aug 20, 2015 5:59 am; edited 2 times in total |
|
Back to top |
|
 |
mqjeff |
Posted: Thu Aug 20, 2015 5:58 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
You can't access the Output message until you've populated it.
Switch your code to read from the Input message, and copy the entire Input tree to the Output tree... And then set fields in the Output tree.
Also. Catch NullPointerExceptions...  _________________ chmod -R ugo-wx / |
|
Back to top |
|
 |
archana123 |
Posted: Thu Aug 20, 2015 6:01 am Post subject: |
|
|
Novice
Joined: 21 Jul 2015 Posts: 14
|
Hi. Thanks for the reply. But I had done that already.
MbMessage outMessage = new MbMessage(inMessage);
Initially I set the input to outMessage and tried to mmodify the outMessage.
But still I am getting the error. I am not able to change the value |
|
Back to top |
|
 |
mqjeff |
Posted: Thu Aug 20, 2015 6:18 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
archana123 wrote: |
Hi. Thanks for the reply. But I had done that already.
MbMessage outMessage = new MbMessage(inMessage);
Initially I set the input to outMessage and tried to mmodify the outMessage.
But still I am getting the error. I am not able to change the value |
The error you're getting is a NullPointerException. That means, most likely, that your xpath didn't match (getFirstChildByPath).
Try wrapping that in a catch for a NullPointerException. And then adjust your XPath until it returns the right thing. _________________ chmod -R ugo-wx / |
|
Back to top |
|
 |
archana123 |
Posted: Fri Aug 21, 2015 8:00 am Post subject: |
|
|
Novice
Joined: 21 Jul 2015 Posts: 14
|
I am able to access the element now. But the problem I am facing is I am not able to modify the value and set it back to the message.
Code: |
MbElement snapArray = outMessage.getRootElement()
.getFirstElementByPath(
"/JSON/Data/Item/Parameter");
System.out.println(snapArray);
List tmpNameList = (List) snapArray.evaluateXPath("Item/Name");
|
Now I try to iterate through my list and get each element
Code: |
for (int i = 0; i < tmpNameList.size(); i++) {
}
|
In the following link,
http://www-01.ibm.com/support/knowledgecenter/SSMKHH_10.0.0/com.ibm.etools.mft.doc/bc28412_.htm?lang=en
It says we can access the element in n array as the following:
Code: |
(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)
)
)
)
)
|
Then access as:
Code: |
inMessage.getRootElement().getFirstElementByPath("/JSON/Data/orders/Item[1]/Item[1]"); // 'thing1'
inMessage.getRootElement().getFirstElementByPath("/JSON/Data/orders/*[2]/*[3]"); // '2.02'
|
But when I try to access my element using
"/JSON/Data/Item/Parameter/Item[1]/Name" , It throws error.
I also tried to access my element as
Code: |
String jsonName = tmpNameList.get(i).toString()
.replace("\n", " ").replace("\r", " ")
.replaceAll("[\\t\\n\\r]+", "").trim()
.replaceAll(" +", " ");
|
Then I get the result as:
MbElement( type: 3000000 name: Name value: Param' )
I need to get the values in each item, modify it and set it back to the same place.
It is not working.
Pls  |
|
Back to top |
|
 |
mqjeff |
Posted: Fri Aug 21, 2015 8:06 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
Updating a field in a JSON message tree is exactly the same as updating a field in any other parser message tree.
The examples should show you how to do this. It's also documented in the Javadoc for the API.
And the reason you get an error trying to access "/JSON/Data/Item/Parameter/Item[1]/Name" is that there is no Name element under your Item element(s). At least in the trace you've shown.
If you're trying to change the fieldname of an element, you will need to use the correct API, and syntax in MB xpath, to do that. _________________ chmod -R ugo-wx / |
|
Back to top |
|
 |
|