|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
 |
|
Creating JSON Array in a JSON Array |
« View previous topic :: View next topic » |
Author |
Message
|
wmbfrz |
Posted: Wed Jul 27, 2016 10:57 pm Post subject: Creating JSON Array in a JSON Array |
|
|
Apprentice
Joined: 08 Jan 2010 Posts: 28
|
Hello,
I am trying to achieve a structure as follows:
Code: |
{
{}
{}
FirstArray[
{
SomeElement1,
SomeElement2,
SecondArray[],
ThirdArray[],
SomeElement3
}
{
SomeElement1,
SomeElement2,
SecondArray[],
ThirdArray[],
SomeElement3
}
]
} |
I have written a code as follow
Code: |
WHILE loopingcondition DO
SET FirstArrayROW.rowData[itemIndex].SomeElement1 = somevalue;
SET FirstArrayROW.rowData[itemIndex].SomeElement1 = somevalue;
Declare SecondArrayROW ROW;
SET SecondArrayROW.rowdata[index].blah1 = blah;
SET SecondArrayROW.rowdata[index].blah2 = blah;
Declare ThirdArrayROW ROW;
SET ThirdArrayROW.rowdata[index].blah1 = blah;
SET ThirdArrayROW.rowdata[index].blah2 = blah;
SET FirstArrayROW.rowData[itemIndex].SecondArray = SecondArrayROW;
SET FirstArrayROW.rowData[itemIndex].SecondArray TYPE = JSON.Array;
SET FirstArrayROW.rowData[itemIndex].ThirdArray = ThirdArrayROW;
SET FirstArrayROW.rowData[itemIndex].ThirdArray TYPE = JSON.Array;
END WHILE;
SET FirstArray.rowData[itemIndex].FirstArray = FirstArrayROW;
SET FirstArray.rowData[itemIndex].FirstArray TYPE = JSON.Array; |
But the output comes as follows
Code: |
{
{}
{}
FirstArray[
{
SomeElement1,
SomeElement2,
SecondArray{},
ThirdArray{},
SomeElement3
}
{
SomeElement1,
SomeElement2,
SecondArray{},
ThirdArray{},
SomeElement3
}
]
} |
Please let me know how can i get the desired output. Discard any thing about looping and element name, as it is only a sample representation. |
|
Back to top |
|
 |
adubya |
Posted: Thu Jul 28, 2016 12:01 am Post subject: |
|
|
Partisan
Joined: 25 Aug 2011 Posts: 377 Location: GU12, UK
|
JSON arrays (when represented in the IIB message tree) need "Item" child elements under which the data structures for that particular array entry are stored. You're not creating these "Item" entries.
Have a look at this Infocenter page which shows the message tree structure (including the Item elements) for a JSON Array.
http://www.ibm.com/support/knowledgecenter/en/SSMKHH_10.0.0/com.ibm.etools.mft.doc/bc28412_.htm
You still have some other work to do but I've not analysed your code in detail to spell out what that is.
Minus points for using an index to reference the message tree though  _________________ Independent Middleware Consultant
andy@knownentity.com |
|
Back to top |
|
 |
wmbfrz |
Posted: Thu Jul 28, 2016 1:52 am Post subject: |
|
|
Apprentice
Joined: 08 Jan 2010 Posts: 28
|
adubya wrote: |
JSON arrays (when represented in the IIB message tree) need "Item" child elements under which the data structures for that particular array entry are stored. You're not creating these "Item" entries.
Have a look at this Infocenter page which shows the message tree structure (including the Item elements) for a JSON Array.
http://www.ibm.com/support/knowledgecenter/en/SSMKHH_10.0.0/com.ibm.etools.mft.doc/bc28412_.htm
You still have some other work to do but I've not analysed your code in detail to spell out what that is.
Minus points for using an index to reference the message tree though  |
Sorry i am not clear with what you have tried to point out. Based on your explanation, the FirstArray should also not be formatted as an array. But it does!
I have also tried replacing
SET FirstArrayROW.rowData[itemIndex].SecondArray TYPE = JSON.Array;
SET FirstArrayROW.rowData[itemIndex].ThirdArray TYPE = JSON.Array;
with the following outside the loop, but it is the same.
SET FirstArray.SecondArray TYPE = JSON.Array;
SET FirstArray.ThirdArray TYPE = JSON.Array; |
|
Back to top |
|
 |
adubya |
Posted: Thu Jul 28, 2016 4:18 am Post subject: |
|
|
Partisan
Joined: 25 Aug 2011 Posts: 377 Location: GU12, UK
|
I'm trying to point out that if you construct a JSON message tree in IIB then for array constructs each array entry needs an "Item" element under the array root element.
I'm not sure why your top level element is being shown as a JSON array but we can't see all the code and don't know how you captured the resultant JSON output.
Did the infocenter link not help ? It shows the "Item" elements in the message tree for the JSON arrays.
If I were creating a simple JSON array message tree then I would use the following (excuse errors, I'm typing this from memory/freehand and haven't executed it)
Code: |
CREATE LASTCHILD OF OutputRoot AS outRef DOMAIN 'JSON';
CREATE LASTCHILD OF outRef AS outRef NAME 'Data';
CREATE FIELD outRef.arrayRoot TYPE JSON.Array;
CREATE LASTCHILD OF outRef.arrayRoot AS itemRef NAME 'Item';
SET itemRef.arrayEntry = 1;
CREATE LASTCHILD OF outRef.arrayRoot AS itemRef NAME 'Item';
SET itemRef.arrayEntry = 2;
CREATE LASTCHILD OF outRef.arrayRoot AS itemRef NAME 'Item';
SET itemRef.arrayEntry = 3;
Should give you a JSON output
"arrayRoot" : [
{ "arrayEntry" : 1 },
{ "arrayEntry" : 2 },
{ "arrayEntry" : 3 }
] |
And if I wanted an embedded array
Code: |
..... same code as above followed by :-
CREATE LASTCHILD OF outRef.arrayRoot AS itemRef NAME 'Item';
CREATE LASTCHILD OF itemRef AS childRef TYPE JSON.Array NAME 'childArray';
CREATE LASTCHILD OF childRef AS itemRef NAME 'Item';
SET itemRef.childEntry = 'A';
CREATE LASTCHILD OF childRef AS itemRef NAME 'Item';
SET itemRef.childEntry = 'B';
Should give you a JSON output
"arrayRoot" : [
{ "arrayEntry" : 1 },
{ "arrayEntry" : 2 },
{ "arrayEntry" : 3 },
{
"childArray" : [
{ "childEntry" : "A" },
{ "childEntry" : "B" }
] }
] |
_________________ Independent Middleware Consultant
andy@knownentity.com |
|
Back to top |
|
 |
adubya |
Posted: Sat Jul 30, 2016 7:37 am Post subject: |
|
|
Partisan
Joined: 25 Aug 2011 Posts: 377 Location: GU12, UK
|
Has a quick play with the ROW approach with JSON and it seems to work as documented.
Code: |
CREATE FUNCTION Main() RETURNS BOOLEAN
BEGIN
CALL CopyMessageHeaders();
DECLARE FirstArrayROW, SecondArrayROW ROW;
DECLARE rowRef REFERENCE TO FirstArrayROW;
DECLARE outRef REFERENCE TO OutputRoot;
CREATE LASTCHILD OF rowRef NAME 'rowData';
SET rowRef.rowData[<].element = 'hello';
CREATE LASTCHILD OF rowRef NAME 'rowData';
SET rowRef.rowData[<].element = 'world';
MOVE rowRef TO SecondArrayROW;
CREATE LASTCHILD OF rowRef NAME 'embeddedRowData';
SET rowRef.embeddedRowData[<].element = 'hello again';
CREATE LASTCHILD OF rowRef NAME 'embeddedRowData';
SET rowRef.embeddedRowData[<].element = 'cruel world';
CREATE LASTCHILD OF OutputRoot AS outRef DOMAIN 'JSON';
CREATE LASTCHILD OF outRef AS outRef NAME 'Data';
SET outRef.row1 = FirstArrayROW;
SET outRef.row1 TYPE = JSON.Array;
SET outRef.row1.rowData[<].embeddedRow = SecondArrayROW;
SET outRef.row1.rowData[<].embeddedRow TYPE = JSON.Array;
RETURN TRUE;
END; |
Produces the following JSON
Code: |
{
"row1": [{
"element": "hello"
},
{
"element": "world",
"embeddedRow": [{
"element": "hello again"
},
{
"element": "cruel world"
}]
}]
} |
I *think* your issue is that you're setting the JSON.Array type attribute on the ROW datastructure and not in a message tree which has a JSON parser associated with it (OutputRoot in my case). _________________ Independent Middleware Consultant
andy@knownentity.com |
|
Back to top |
|
 |
vickypaiyaa |
Posted: Tue Jan 29, 2019 10:34 pm Post subject: json array object creation |
|
|
Newbie
Joined: 27 Sep 2018 Posts: 1
|
i'm trying to create json array object as below:
{
"type": "order1",
"users": [
{
"credits": "sender",
"params": {
"ordertype": "report",
"orderno": "123",
"orderdate": "01-01-2019"
}
}
]
}
esql code:
SET OutputRoot.JSON.Data.type = 'order1';
CREATE FIELD OutputRoot.JSON.Data.users IDENTITY(JSON.Array);
SET OutputRoot.JSON.Data.users.Item.credits = 'sender';
CREATE LASTCHILD OF OutputRoot.JSON.Data.users.params NAME 'params';
SET OutputRoot.JSON.Data.users.params.params[1].ordertype = 'report';
SET OutputRoot.JSON.Data.users.params.params[1].orderno = '123';
SET OutputRoot.JSON.Data.users.params.params[1].orderdate = '01-01-2019';
but im getting the below json array:
{
"type": "order1",
"users": [
{"credits": "sender"},
{"params": {
"ordertype": "report",
"orderno": "123",
"orderdate": "01-01-2019"
}}
]
}
Anyone help on this how to resolve and what i did error in that?> |
|
Back to top |
|
 |
timber |
Posted: Wed Jan 30, 2019 2:59 am Post subject: |
|
|
 Grand Master
Joined: 25 Aug 2015 Posts: 1292
|
1. Create a text file containing the JSON that you want to produce
2. Create a small message flow that parses your file. Make sure that it contains a Trace node with pattern ${Root}
3. Look at the Trace node output, and then write ESQL to produce exactly the same message tree. |
|
Back to top |
|
 |
mpong |
Posted: Thu Jan 31, 2019 1:34 pm Post subject: |
|
|
Disciple
Joined: 22 Jan 2010 Posts: 164
|
Can you try this??
Quote: |
DECLARE i int 1;
SET OutputRoot.JSON.Data.type = 'order1';
CREATE FIELD OutputRoot.JSON.Data.users IDENTITY(JSON.Array);
SET OutputRoot.JSON.Data.users.Item[i].credits='sender';
SET OutputRoot.JSON.Data.users.Item[i].params.ordertype='report';
SET OutputRoot.JSON.Data.users.Item[i].params.orderno='123';
SET OutputRoot.JSON.Data.users.Item[i].params.orderdate='01-01-2019'; |
SET OutputRoot.JSON.Data.type = 'order1' (type is namevalue under object JSON.Data)
CREATE FIELD OutputRoot.JSON.Data.users IDENTITY(JSON.Array) ( just a JSON array)
SET OutputRoot.JSON.Data.users.Item[i].credits='sender'; (credits is a Namevalue under item object)
OutputRoot.JSON.Data.users.Item[i].params.ordertype='report'; (ordertype is a namevalue under object params within users array) |
|
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
|
|
|
|