|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
 |
|
Appending new object to existing JSON.Array |
« View previous topic :: View next topic » |
Author |
Message
|
Ksawery |
Posted: Fri Mar 14, 2025 2:19 am Post subject: Appending new object to existing JSON.Array |
|
|
Newbie
Joined: 12 Oct 2022 Posts: 6
|
I need some help with the following issue. Currently, my code generates the following response:
Code: |
{
"id": 39,
"localizationName": "Bublin",
"localizationId": 2,
"warehouseCode": "K03",
"warehouseName": "Parts warehouse",
"status": "0",
"gidNumer": 4,
"documentseries": []
},
{
"id": 1,
"name": "JOB",
"gidnumer": 144
},
{
"id": 3,
"name": "BREAKS",
"gidnumer": 320
},
{
"id": 5,
"name": "BOTE",
"gidnumer": 156
},
{
"id": 42,
"localizationName": "London",
"localizationId": 5,
"warehouseCode": "MAG",
"warehouseName": "Common warehouse",
"status": "0",
"gidNumer": 1,
"documentseries": []
},
{
"id": 1,
"name": "JOB",
"gidnumer": 144
},
|
However, as you can see, it is incorrect because the array 'documentseries' is created, but the objects are instantiated separately outside of the documentseries array. As an example, here is the API Response schema I need to create:
Code: |
[
{
"id": 1,
"localizationName": "value",
"localizationId": 1,
"warehouseCode": "value",
"warehouseName": "value",
"status": "value",
"gidNumer": 23,
"documentSeries": [
{
"id": 1,
"name": "value",
"gidnumer": 1
}
]
}
] |
What I want to achieve is that after iteration, the id, name, and gidnumber objects are placed inside the documentseries array. Below is the code I am currently using:
Code: |
DECLARE myRef REFERENCE TO Environment.Variables.relation[1];
DECLARE currentId CHARACTER '';
CREATE FIELD OutputRoot.JSON.Data IDENTITY(JSON.Array)Data;
-- START test kod
DECLARE jsonRef REFERENCE TO OutputRoot.JSON.Data;
DECLARE docRef REFERENCE TO OutputRoot.JSON.Data;
WHILE LASTMOVE(myRef) DO
IF currentId <> myRef.id THEN
CREATE LASTCHILD OF OutputRoot.JSON.Data IDENTITY(JSON.Object);
DECLARE jsonRef REFERENCE TO OutputRoot.JSON.Data;
MOVE jsonRef LASTCHILD;
SET jsonRef.id = myRef.id;
SET jsonRef.localizationName = myRef.localizationName;
SET jsonRef.localizationId = myRef.localizationId;
SET jsonRef.warehouseCode = myRef.warehouseCode;
SET jsonRef.warehouseName = myRef.warehouseName;
SET jsonRef.status = myRef.status;
SET jsonRef.gidNumer = myRef.gidNumer;
IF myRef.documentId IS NOT NULL THEN
CREATE LASTCHILD OF jsonRef IDENTITY(JSON.Array) documentseries;
END IF;
SET currentId = myRef.id;
END IF;
IF myRef.documentId IS NOT NULL THEN
DECLARE docRef REFERENCE TO jsonRef.documentseries;
CREATE LASTCHILD OF docRef IDENTITY(JSON.Object);
MOVE docRef LASTCHILD;
SET docRef.id = myRef.documentId;
SET docRef.name = myRef.documentName;
SET docRef.gidnumer = myRef.documentGidNumer;
END IF;
MOVE myRef NEXTSIBLING;
END WHILE;
|
If you see how to fix this and could kindly show me the solution, I would be very grateful. I've been struggling with this for three weeks now without achieving the expected result. Thanks! |
|
Back to top |
|
 |
mgk |
Posted: Sat Mar 15, 2025 5:13 am Post subject: |
|
|
 Padawan
Joined: 31 Jul 2003 Posts: 1642
|
It is hard to be sure this is what you need without an actual test input message, but there were several errors where you redeclared variables in an inner scope which will hide the outer definition and is causing problems.
Therefore, I've made a few tweaks and I think this should be much closer to the output what you are looking for:
Code: |
DECLARE myRef REFERENCE TO Environment.Variables.relation[1];
DECLARE currentId CHARACTER '';
CREATE FIELD OutputRoot.JSON.Data TYPE JSON.Array;
DECLARE jsonRef REFERENCE TO OutputRoot.JSON.Data;
WHILE LASTMOVE(myRef) DO
IF currentId <> myRef.id THEN
CREATE LASTCHILD OF OutputRoot.JSON.Data AS jsonRef TYPE JSON.Object;
MOVE jsonRef LASTCHILD;
SET jsonRef.id = myRef.id;
SET jsonRef.localizationName = myRef.localizationName;
SET jsonRef.localizationId = myRef.localizationId;
SET jsonRef.warehouseCode = myRef.warehouseCode;
SET jsonRef.warehouseName = myRef.warehouseName;
SET jsonRef.status = myRef.status;
SET jsonRef.gidNumer = myRef.gidNumer;
IF myRef.documentId IS NOT NULL THEN
CREATE LASTCHILD OF jsonRef TYPE JSON.Array NAME 'documentseries';
END IF;
SET currentId = myRef.id;
END IF;
IF myRef.documentId IS NOT NULL THEN
DECLARE docRef REFERENCE TO jsonRef.documentseries;
CREATE LASTCHILD OF docRef AS docRef TYPE JSON.Object;
SET docRef.id = myRef.documentId;
SET docRef.name = myRef.documentName;
SET docRef.gidnumer = myRef.documentGidNumer;
END IF;
MOVE myRef NEXTSIBLING;
END WHILE;
|
I hope this helps. _________________ 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 |
|
 |
Ksawery |
Posted: Sun Mar 16, 2025 11:50 pm Post subject: |
|
|
Newbie
Joined: 12 Oct 2022 Posts: 6
|
mgk wrote: |
...but there were several errors where you redeclared variables in an inner scope which will hide the outer definition and is causing problems.... |
Yes, this was indeed the case. I personally made changes to this issue and found the right way of implementation. Now I was able to achieve the expected answer. Thank you very much for your help! |
|
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
|
|
|
|