|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
 |
|
Java compute node: Extracting data from Json Array |
« View previous topic :: View next topic » |
Author |
Message
|
raghuiib |
Posted: Sat Oct 27, 2018 10:09 am Post subject: Java compute node: Extracting data from Json Array |
|
|
Novice
Joined: 01 Oct 2018 Posts: 11
|
Hello,
IIB Version: 10.0.0.13
I have few years of ESQL experience and very new to Java coding. I am running into an issue with extracting data from Json array. Very likely I am missing something in my Java code.
My input message:
Code: |
{
"header": {
"_Message_ID": "123",
"_Type": "bravo"
},
"payload": {
"field1": "value1",
"field2": "value2",
"rulesApplied": ["R1","R2","R3"]
}
} |
Here is the message tree before code execution:
Code: |
Message
JSON
Data
header
_Message_ID:CHARACTER:123
_Type:CHARACTER:bravo
payload
field1:CHARACTER:value1
field2:CHARACTER:value2
rulesApplied
Item:CHARACTER:R1
Item:CHARACTER:R2
Item:CHARACTER:R3 |
Java code:
Code: |
PreparedStatement ps = conn.prepareStatement(sql);
MbElement refPayload = inMessage.getRootElement().getFirstElementByPath("JSON/Data/payload");
ps.setString(1,(refPayload.getFirstElementByPath("/rulesApplied/Item[1]").getValueAsString())); |
The third line of code where I reference Item[1] results in NULL pointer exception.
To access data in Json array, I referred to this IBM documentation:
https://www.ibm.com/support/knowledgecenter/en/SSMKHH_10.0.0/com.ibm.etools.mft.doc/bc28412_.htm
Hoping someone in this forum can help me with what I am missing.
Thanks. |
|
Back to top |
|
 |
timber |
Posted: Sat Oct 27, 2018 1:03 pm Post subject: |
|
|
 Grand Master
Joined: 25 Aug 2015 Posts: 1292
|
Nothing obviously incorrect in your Java. I suggest that you follow the usual debugging techniques. Simplify the code until the location of the error becomes obvious. I would start by checking that you can access the JSON node, then the Data node.
Alternatively, you could stop trying to use the old-fashioned Java API (which isn't particularly user-friendly) and generate JAXB bindings from your schema. That will provide you with a much neater API onto the message tree. |
|
Back to top |
|
 |
raghuiib |
Posted: Sun Oct 28, 2018 5:58 am Post subject: |
|
|
Novice
Joined: 01 Oct 2018 Posts: 11
|
I am able to access other data elements, both in header and payload, that are not part of the array. It is just the array elements that is the issue.
I am new to Java and on a learning curve. Will explore JAXB. Thank you. |
|
Back to top |
|
 |
martinb |
Posted: Mon Oct 29, 2018 9:27 am Post subject: |
|
|
Master
Joined: 09 Nov 2006 Posts: 210 Location: UK
|
Hi
I think the problem is you're using an absolute path here
Code: |
refPayload.getFirstElementByPath("/rulesApplied/Item[1]")
|
As you are calling getFirstElementByPath() from an element part way down the tree, you need to be giving a relative Xpath path expression as the argument. I think you should be using
Code: |
refPayload.getFirstElementByPath("rulesApplied/Item[1]")
|
HTH |
|
Back to top |
|
 |
raghuiib |
Posted: Mon Oct 29, 2018 12:40 pm Post subject: |
|
|
Novice
Joined: 01 Oct 2018 Posts: 11
|
Hello Martin B,
I realized it and tested it with the relative path. Still got the null pointer error.
Code: |
ps.setString(1,(refPayload.getFirstElementByPath("rulesApplied/Item[1]").getValueAsString())); |
I have so much more appreciation for ESQL now.
[/code] |
|
Back to top |
|
 |
|
|
 |
|
Page 1 of 1 |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|