|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
 |
|
manipulate JSON message in Java compute node |
« View previous topic :: View next topic » |
Author |
Message
|
wmqstankela |
Posted: Wed Mar 14, 2018 4:43 am Post subject: manipulate JSON message in Java compute node |
|
|
Voyager
Joined: 29 Feb 2016 Posts: 94
|
Hi all,
I need some help. I have REST service and my input is JSON with array of attributes:
Code: |
(0x01000000:Object):JSON = ( ['json' : 0x23f317914f0]
(0x01000000:Object):Data = (
(0x01001000:Array ):attributes = (
(0x01000000:Object):Item = (
(0x01000000:Object):attribute = (
(0x03000000:NameValue):name = 'name' (CHARACTER)
(0x03000000:NameValue):value = 'John' (CHARACTER)
)
)
(0x01000000:Object):Item = (
(0x01000000:Object):attribute = (
(0x03000000:NameValue):name = 'lastname' (CHARACTER)
(0x03000000:NameValue):value = 'Doe' (CHARACTER)
)
)
(0x01000000:Object):Item = (
(0x01000000:Object):attribute = (
(0x03000000:NameValue):name = 'mail' (CHARACTER)
(0x03000000:NameValue):value = 'john.doe@example.com' (CHARACTER)
)
)
)
)
|
Number of attributes is dynamic parameter, and I don't know how many attributes client will send to service. How to iterate through input JSON message in Java compute node and populate attributes in list of attribute?
Thanks in advance! |
|
Back to top |
|
 |
timber |
Posted: Wed Mar 14, 2018 12:33 pm Post subject: |
|
|
 Grand Master
Joined: 25 Aug 2015 Posts: 1292
|
Don't want to sound unhelpful but...what have you tried? There are quite a few answers, and it's hard to know where to start. And the 'correct' answer may depend on requirements that you haven't mentioned yet. |
|
Back to top |
|
 |
wmqstankela |
Posted: Thu Mar 15, 2018 5:17 am Post subject: |
|
|
Voyager
Joined: 29 Feb 2016 Posts: 94
|
I've done it, thanks timber anyway. |
|
Back to top |
|
 |
Vitor |
Posted: Thu Mar 15, 2018 5:43 am Post subject: |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
wmqstankela wrote: |
I've done it, thanks timber anyway. |
Care to post your solution for the benefit of future readers? _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
wmqstankela |
Posted: Thu Mar 15, 2018 6:10 am Post subject: |
|
|
Voyager
Joined: 29 Feb 2016 Posts: 94
|
I've put JSON message in MbElement and then iterate through that element using while loop and getNextSibling() method, until I reached getLastChild element.
First need to manipulate with first attribute, and then iterate through the rest of them. Something like that:
Code: |
List<Attribute> attributes = new ArrayList<Attribute>();
MbElement paramAttributes = inJsonRoot.getFirstElementByPath("/JSON/Data/attributes");
MbElement firstItem = paramAttributes.getFirstChild();
MbElement lastItem = paramAttributes.getLastChild();
boolean isLast= false;
while !(isLast){
firstItem = firstItem.getNextSibling();
if (firstItem.is(lastItem)){ isLast= true; }
a = new Attribute(firstItem.getFirstChild().getFirstChild().getValueAsString(), firstItem.getFirstChild().getLastChild().getValueAsString());
attributes.add(a);
}
|
|
|
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
|
|
|
|