Author |
Message
|
Marc456 |
Posted: Wed May 11, 2016 11:35 am Post subject: iib9 - Create JSON arrays from an Environment variable |
|
|
Newbie
Joined: 11 May 2016 Posts: 8
|
Hi there - first post, however, you guys saved a lot of days already for me. But this one is driving me nuts!
I need to create a JSON message which needs to look like the following:
{"items": [
{
"Valuation": "EUR",
"Total": "0.00",
"nextArray": [
{
"name1": "Value1",
"name2": "Value2"
},
{
"name1": "value3",
"name2": "value3"
}
]
}
]}
Above will repeat as many times as needed (based on some results). Above result is the result of 1 loop of several subflows.
No big news over here I would say. In fact, I do know how to create Arrays in the OutputRoot.JSON.Data.
However:
I need to create the entire message in the (local) environment first, before I can create a JSON out of it (read: before i can promote it to the Output Root. I cannot built the message in the OutputRoot earlier, becauase I need to loop through more subflows again (with a next ID), causing the OutputRoot to be 'deleted' every time I start a new loop to the subflows to gather the information for the next item). Therefor I built the correct message in the (local)Environment first, which worked perfectly (ofcourse) when it needed to be promoted to XMLNSC later on.
However, when I promote the Environment variable to OutputRoot.JSON.Data by doing the following:
Code: |
SET OutputRoot.JSON.Data = Environment.Variable.responseMessage; |
I am not getting the arrays automatically. I understand that. Its no [ ] at all, only { }
(funny thing is that SOAPUI is translating it correctly :S)
I can create the first array (items) as well by the following code:
Code: |
CREATE FIELD OutputRoot.JSON.Data.items IDENTITY(JSON.Array)items;
SET OutputRoot.JSON.Data.items[] = Environment.Variable.responseMessage[];
|
Also not an issue... However. How can I also declare the nextArray within the items array as an array?
Is that possible at all?
Can JSON (meta) data (like Array) be stored in a Environment variable?
I also have found a script which will translate an XML to JSON, with respect to the arrays. However, this will be used for an app response, and I want to avoid too many loops, and take the most effective way to create the JSON.
Thank you so much to have taken the patience to read this / answer this. I am starting to get an headache
For more questions I will be available!
(also when i found the answer).
Cheers! |
|
Back to top |
|
 |
timber |
Posted: Wed May 11, 2016 2:27 pm Post subject: |
|
|
 Grand Master
Joined: 25 Aug 2015 Posts: 1292
|
When you create the parent element in Environment, make sure that you explicitly assign a domain (a parser) to it. In this case, you need to do something like
Code: |
CREATE LASTCHILD OF Environment.Variables TYPE Name NAME 'myJSONstuff' DOMAIN 'JSON'; |
If you don't do that, the special value that indicates that it's a JSON array ( technical name is 'field type' ) will be lost when you copy from Environment into OutputRoot. Why? Because they are using different parser ( domain ) and a field type is only meaningful for a specific parser (domain).
Quote: |
you guys saved a lot of days already for me |
You just saved us a lot of to-ing and fro-ing by explaining your problem properly at the first attempt. Thanks! |
|
Back to top |
|
 |
Marc456 |
Posted: Thu May 12, 2016 12:57 am Post subject: |
|
|
Newbie
Joined: 11 May 2016 Posts: 8
|
Thanks timber! Much appreciated the help and compliment you gave!
Bottomline, that this was indeed the trick I just needed (besides the fact that I now realize that the so many times seen 'Item' is a field type from the JSON parser )
My end result is now like this:
Code: |
--First set the JSON parser once
IF cntItm= 1 THEN
CREATE LASTCHILD OF Environment.Variable DOMAIN('JSON') TYPE Name NAME 'responseMessage';
END IF; |
(changed the order of the CREATE )
Then start building the Array within the Environment by first determine the first array on the name items (this items must reflect in my end result)
Code: |
CREATE FIELD Environment.Variable.responseMessage.array1 IDENTITY(JSON.Array)array1;
SET Environment.Variable.responseMessage.array1.Item[cntItm].nameValue1 = varNameOfArray1; |
Setting up the 2nd array within the 1st array:
Code: |
CREATE FIELD Environment.Variable.responseMessage.array1.Item[cntItm].array2 IDENTITY(JSON.Array)array2;
SET Environment.Variable.responseMessage.array1.Item[cntItm].array2.Item[cntItm2].nameValue2 = varNameOfArray2; |
Resulting in the JSON message:
Code: |
{
"array1": [
{
"nameValue1": "This is value for array 1",
"array2": [
{
"nameValue2": "This is value for array 2"
},
{
"nameValue2": "This is value for array 2"
}
]
},
{
"nameValue1": "This is value for array 1",
"array2": [
{
"nameValue2": "This is value for array 2"
},
{
"nameValue2": "This is value for array 2"
}
]
},
]
} |
Thanks again and I hope to help you/someone next time!
Cheers! |
|
Back to top |
|
 |
mayheminMQ |
Posted: Fri May 13, 2016 1:21 am Post subject: |
|
|
 Voyager
Joined: 04 Sep 2012 Posts: 77 Location: UK beyond the meadows of RocknRoll
|
Sorry if this is forking the main post but I have been scratching my head thinking can we do the same with Row variables? |
|
Back to top |
|
 |
fjb_saper |
Posted: Fri May 13, 2016 1:35 am Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
Have you tried? what were the results?  _________________ MQ & Broker admin |
|
Back to top |
|
 |
mayheminMQ |
Posted: Fri May 13, 2016 1:45 am Post subject: |
|
|
 Voyager
Joined: 04 Sep 2012 Posts: 77 Location: UK beyond the meadows of RocknRoll
|
No I did not (lazy lazy me) but now that no one has said NO, I am going to try and get back with some results.
Update - Mindblown and yes I did create a JSON structure with a ROW variable under domain JSON with a nested Array.
 _________________ A Colorblind man may appear disadvantaged but he always sees more than just colors... |
|
Back to top |
|
 |
mgk |
Posted: Fri May 13, 2016 7:07 am Post subject: |
|
|
 Padawan
Joined: 31 Jul 2003 Posts: 1642
|
Quote: |
I did create a JSON structure with a ROW variable under domain JSON with a nested Array |
Yes, that's because a ROW variable is actually a user defined tree - like InputRoot etc but with a different name
Kind regards, _________________ MGK
The postings I make on this site are my own and don't necessarily represent IBM's positions, strategies or opinions. |
|
Back to top |
|
 |
wmbfrz |
Posted: Thu Jul 28, 2016 9:25 am Post subject: |
|
|
Apprentice
Joined: 08 Jan 2010 Posts: 28
|
mayheminMQ wrote: |
No I did not (lazy lazy me) but now that no one has said NO, I am going to try and get back with some results.
Update - Mindblown and yes I did create a JSON structure with a ROW variable under domain JSON with a nested Array.
 |
Can you please have a look at this thread and let me know what did i missed or messed?
http://www.mqseries.net/phpBB2/viewtopic.php?p=409244#409244 |
|
Back to top |
|
 |
adubya |
Posted: Thu Jul 28, 2016 10:55 am Post subject: |
|
|
Partisan
Joined: 25 Aug 2011 Posts: 377 Location: GU12, UK
|
You're missing the fact that you need to create "Item" elements in the IIB message tree for each JSON array entry.
There's no way round that, it's what you have to do
I spent a happy week or two writing subflows to convert XML -> JSON and vice versa in IIB. I'm not making this stuff up
Look at the code above ^^^^ Item elements aplenty! _________________ Independent Middleware Consultant
andy@knownentity.com |
|
Back to top |
|
 |
wmbfrz |
Posted: Fri Jul 29, 2016 2:09 am Post subject: |
|
|
Apprentice
Joined: 08 Jan 2010 Posts: 28
|
adubya wrote: |
You're missing the fact that you need to create "Item" elements in the IIB message tree for each JSON array entry.
There's no way round that, it's what you have to do
I spent a happy week or two writing subflows to convert XML -> JSON and vice versa in IIB. I'm not making this stuff up
Look at the code above ^^^^ Item elements aplenty! |
Yes i know it can be done with what you are saying but i am not comfortable with the notion that it is the only way. I have created JSON array for other req with row type approach and it works fine. Similar thing is mentioned here in info center
https://www.ibm.com/support/knowledgecenter/en/SSMKHH_10.0.0/com.ibm.etools.mft.doc/bc40070_.htm
I have to loop and create array in iterative manner for which i find row element suitable but am at lost how to achieve it. mayheminMQ said he has done it so requested him to clarify. |
|
Back to top |
|
 |
adubya |
Posted: Fri Jul 29, 2016 2:29 am Post subject: |
|
|
Partisan
Joined: 25 Aug 2011 Posts: 377 Location: GU12, UK
|
Ah, I see. My apologies, looks like the ROW approach makes life easier. _________________ Independent Middleware Consultant
andy@knownentity.com |
|
Back to top |
|
 |
wmbfrz |
Posted: Fri Jul 29, 2016 3:54 am Post subject: |
|
|
Apprentice
Joined: 08 Jan 2010 Posts: 28
|
adubya wrote: |
Ah, I see. My apologies, looks like the ROW approach makes life easier. |
Np. But i think i may have given you something to make another happy weekend  |
|
Back to top |
|
 |
|