ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » Java compute node: Extracting data from Json Array

Post new topic  Reply to topic
 Java compute node: Extracting data from Json Array « View previous topic :: View next topic » 
Author Message
raghuiib
PostPosted: Sat Oct 27, 2018 10:09 am    Post subject: Java compute node: Extracting data from Json Array Reply with quote

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
View user's profile Send private message
timber
PostPosted: Sat Oct 27, 2018 1:03 pm    Post subject: Reply with quote

Grand Master

Joined: 25 Aug 2015
Posts: 1280

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
View user's profile Send private message
raghuiib
PostPosted: Sun Oct 28, 2018 5:58 am    Post subject: Reply with quote

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
View user's profile Send private message
martinb
PostPosted: Mon Oct 29, 2018 9:27 am    Post subject: Reply with quote

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
View user's profile Send private message
raghuiib
PostPosted: Mon Oct 29, 2018 12:40 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » Java compute node: Extracting data from Json Array
Jump to:  



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
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.