|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
![Reply to topic](templates/DAJ_Glass/images/lang_english/reply.gif) |
|
create "complex" JSON object with java |
« View previous topic :: View next topic » |
Author |
Message
|
IIBn00b |
Posted: Tue Nov 19, 2019 4:16 am Post subject: create "complex" JSON object with java |
|
|
Newbie
Joined: 19 Nov 2019 Posts: 7
|
I have found documentation on how to create this JSON structure:
with Java.
Java code:
Code: |
MbElement outRoot = outMessage.getRootElement();
MbElement outJsonRoot = outRoot.createElementAsLastChild(MbJSON.PARSER_NAME);
MbElement outJsonData = outJsonRoot.createElementAsLastChild(MbElement.TYPE_NAME, MbJSON.DATA_ELEMENT_NAME, null);
MbElement outJsonTest = outJsonData.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "name", "value"); |
I also found how to create this JSON structure with Java:
Code: |
[
{
"name":"value",
"name2": {
"subname0":"sub value0",
"subname1":"sub value1"
}
}
] |
Java code:
Code: |
MbElement outRoot = outMessage.getRootElement();
MbElement outJsonRoot = outRoot.createElementAsLastChild(MbJSON.PARSER_NAME);
MbElement outJsonProducts = outJsonRoot.createElementAsLastChild(MbElement.TYPE_NAME, MbJSON.DATA_ELEMENT_NAME, null);
MbElement elementArray = outJsonProducts.createElementAsLastChild(MbJSON.ARRAY, MbJSON.DATA_ELEMENT_NAME, null);
while (ceKeysIterator2.hasNext()) {
String key = ceKeysIterator2.next();
ConsumentenEenheid ce = consumentenEenheidMap.get(key);
Map<String, String> legalInfoMapData = legalInfoMap.get(ce.getProductID()).getMap();
if(legalInfoMapData.isEmpty()) {
continue;
}
MbElement outJsonData = elementArray.createElementAsLastChild(MbElement.TYPE_NAME, MbJSON.ARRAY_ITEM_NAME, null);
MbElement skuData = outJsonData.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "name", "value");
Set<String> legalKeys = legalInfoMapData.keySet();
Iterator<String> legalIterator = legalKeys.iterator();
MbElement legalInfoData = outJsonData.createElementAsLastChild(MbElement.TYPE_NAME, "name2", null);
int x = 0;
while(x <= 1) {
MbElement legalInfoDataElement = legalInfoData.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "subname" + x, "subvalue" + x);
x = x + 1;
}
}
|
What I'm unable to create is a combination of the two:
Code: |
{
"name": [
{
"foo": {
"bar": "one",
"bar2": "two"
},
"baz": "three"
}
]
} |
In short: How can I set the value of a JSON node to an array instead of a simple type (Like a string)?
I know how to do it in normal Java. I don't know how to do it in the IIB environment.
This is what I already tried:
Code: |
MbElement outRoot = outMessage.getRootElement();
MbElement outJsonRoot = outRoot.createElementAsLastChild(MbJSON.PARSER_NAME);
MbElement outJsonProducts = outJsonRoot.createElementAsLastChild(MbElement.TYPE_NAME, MbJSON.DATA_ELEMENT_NAME, null);
MbElement outJsonTest =
outJsonProducts.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "products", "Hi");
MbElement elementArray = outJsonTest.createElementAsLastChild(MbJSON.ARRAY, MbJSON.DATA_ELEMENT_NAME, null);
Set<String> ceKeys2 = consumentenEenheidMap.keySet();
Iterator<String> ceKeysIterator2 = ceKeys2.iterator();
while (ceKeysIterator2.hasNext()) {
String key = ceKeysIterator2.next();
ConsumentenEenheid ce = consumentenEenheidMap.get(key);
Map<String, String> legalInfoMapData = legalInfoMap.get(ce.getProductID()).getMap();
if(legalInfoMapData.isEmpty()) {
continue;
}
MbElement outJsonData = elementArray.createElementAsLastChild(MbElement.TYPE_NAME, MbJSON.ARRAY_ITEM_NAME, null);
MbElement skuData = outJsonData.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "sku", ce.getCeNummer());
Set<String> legalKeys = legalInfoMapData.keySet();
Iterator<String> legalIterator = legalKeys.iterator();
MbElement legalInfoData = outJsonData.createElementAsLastChild(MbElement.TYPE_NAME, "legalInfo", null);
while(legalIterator.hasNext()) {
String theLegalKey = legalIterator.next();
MbElement legalInfoDataElement = legalInfoData.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, theLegalKey, legalInfoMapData.get(theLegalKey));
}
}
outRoot.getLastChild().getFirstElementByPath("/JSON/Data/products").setValue(elementArray);
|
This results in a JSON structure that looks like this:
Code: |
{
"products": "Hi",
Data
[
Item {
"foo": {
"bar": "one",
"bar2": "two"
},
"baz": "three"
}
]
} |
The values in above array example are just used as example and not representing the values generated by the above code. The structure is what will be generated by the above code.
I was hoping that this line:
Code: |
outRoot.getLastChild().getFirstElementByPath("/JSON/Data/products").setValue(elementArray); |
would replace the "Hi" with the needed array.
Last edited by IIBn00b on Wed Nov 20, 2019 5:15 am; edited 3 times in total |
|
Back to top |
|
![](templates/DAJ_Glass/images/spacer.gif) |
fjb_saper |
Posted: Tue Nov 19, 2019 5:39 am Post subject: |
|
|
![](images/avatars/gallery/misc/2.gif) Grand High Poobah
Joined: 18 Nov 2003 Posts: 20697 Location: LI,NY
|
Moved to broker forum. _________________ MQ & Broker admin |
|
Back to top |
|
![](templates/DAJ_Glass/images/spacer.gif) |
Vitor |
Posted: Tue Nov 19, 2019 5:48 am Post subject: Re: create "complex" JSON object with java |
|
|
![](images/avatars/905340816499abc1cdfb07.jpg) Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
IIBn00b wrote: |
What I'm unable to create is a combination of the two:
Code: |
{
"name": [
{
"foo": {
"bar": "one",
"bar2": "two"
},
"baz": "three"
}
]
} |
In short: How can I set the value of a JSON node to an array instead of a simple type (Like a string)? |
Disclaimer: I'm not a Java person.
What happened when you tried the obvious (creating the array as the LastChild of the desired parent node)? Did it fail, give you the wrong result, what? It's better if you tell us what you tried that didn't work for you as this stops us suggesting it.
Better information, better advice. _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
![](templates/DAJ_Glass/images/spacer.gif) |
IIBn00b |
Posted: Tue Nov 19, 2019 11:36 am Post subject: |
|
|
Newbie
Joined: 19 Nov 2019 Posts: 7
|
I added the code I already tried to my post. |
|
Back to top |
|
![](templates/DAJ_Glass/images/spacer.gif) |
Vitor |
Posted: Tue Nov 19, 2019 12:06 pm Post subject: |
|
|
![](images/avatars/905340816499abc1cdfb07.jpg) Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
Ok, still not a Java person.
Why not build the array as the last child of the needed parent, with array elements as siblings? _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
![](templates/DAJ_Glass/images/spacer.gif) |
IIBn00b |
Posted: Tue Nov 19, 2019 12:20 pm Post subject: |
|
|
Newbie
Joined: 19 Nov 2019 Posts: 7
|
Vitor wrote: |
Ok, still not a Java person.
Why not build the array as the last child of the needed parent, with array elements as siblings? |
The problem is that I don't know how.
I Thought I did that with:
Code: |
MbElement outJsonTest = outJsonProducts.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "products", "Hi");
MbElement elementArray = outJsonTest.createElementAsLastChild(MbJSON.ARRAY, MbJSON.DATA_ELEMENT_NAME, null); |
outJsonTest is the element that should contain the array as a value instead of the value "Hi".
by calling outJsonTest.createElementAsLastChild(...) I hoped it would do just that. Unfortunately it doesn't.
Later on in the code (the last line) I try:
Code: |
outRoot.getLastChild().getFirstElementByPath("/JSON/Data/products").setValue(elementArray); |
In the hope it would replace the "Hi" value with the elementArray. Again not the solution... |
|
Back to top |
|
![](templates/DAJ_Glass/images/spacer.gif) |
Vitor |
Posted: Wed Nov 20, 2019 6:36 am Post subject: |
|
|
![](images/avatars/905340816499abc1cdfb07.jpg) Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
Rapidly moving to the point where you need a Java person......
That code won't replace "Hi" as you're creating the array as a child of it. What you should have got (if I'm squinting at the code correctly) is the array as a child of it in the foo / bar1 / bar2 shape you want.
What did you get? _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
![](templates/DAJ_Glass/images/spacer.gif) |
timber |
Posted: Wed Nov 20, 2019 7:05 am Post subject: |
|
|
![](images/avatars/53508560955fb354bd4bd0.jpg) Grand Master
Joined: 25 Aug 2015 Posts: 1280
|
The secret is to get two things correct
- the structure of the message tree
- the parser-specific field types in the message tree
The first step is to know what the message tree should look like. To do that:
- Create a test message flow with a FileInput node and a Trace node
- Set the Pattern property on the Trace node to ${Root}
- Send your JSON message through this test flow
The Trace node output will show you exactly what the message tree needs to look like.
The second step is to adjust your Java code so that it builds the exact same message tree. That will mean setting some of the nodes to be JSON arrays. You must use method MbElement.setSpecificType for that - you cannot use MbJSON.ARRAY in the MbElement.createElementAsLastChild method (you can, but it will not work). |
|
Back to top |
|
![](templates/DAJ_Glass/images/spacer.gif) |
IIBn00b |
Posted: Fri Nov 22, 2019 2:39 am Post subject: |
|
|
Newbie
Joined: 19 Nov 2019 Posts: 7
|
timber wrote: |
The secret is to get two things correct
- the structure of the message tree
- the parser-specific field types in the message tree
The first step is to know what the message tree should look like. To do that:
- Create a test message flow with a FileInput node and a Trace node
- Set the Pattern property on the Trace node to ${Root}
- Send your JSON message through this test flow
The Trace node output will show you exactly what the message tree needs to look like.
The second step is to adjust your Java code so that it builds the exact same message tree. That will mean setting some of the nodes to be JSON arrays. You must use method MbElement.setSpecificType for that - you cannot use MbJSON.ARRAY in the MbElement.createElementAsLastChild method (you can, but it will not work). |
Using MbElement.setSpecificType did the trick. Thanks. |
|
Back to top |
|
![](templates/DAJ_Glass/images/spacer.gif) |
|
|
![Reply to topic](templates/DAJ_Glass/images/lang_english/reply.gif) |
|
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
|
|
|
|