|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
 |
|
Adding items to a JSON array |
« View previous topic :: View next topic » |
Author |
Message
|
petervh1 |
Posted: Tue Feb 18, 2025 11:43 pm Post subject: Adding items to a JSON array |
|
|
Centurion
Joined: 19 Apr 2010 Posts: 135
|
Hi
I need to create a JSON array and then add 'sets' of elements to this array. The problem I'm facing is that I need to be able to add these new elements from various different places in my ESQL code. This means that if I use this:
Code: |
SET OutputRoot.JSON.Data.response.promotions.Item[o] = Environment.DSLResultSet[p].InputProduct;
.
.
|
then my counter [o] goes haywire because I can't keep track of the subscript value (because the elements are being added from different places in the code)
I tried this at the different places in the ESQL code:
Code: |
CREATE LASTCHILD OF OutputRoot.JSON.Data.response.promotions.Item TYPE NameValue Name 'currentRetailStorePrice' VALUE Environment.DSLResultSet[p].InputProduct.currentRetailStorePrice;
.
.
|
but then I get a single list of elements in the array like this:
Code: |
{
"response": {
"promotions": [
{
"currentRetailStorePrice": "39.99",
"isoCurrencyCode": "ZAR",
"isPromotionalPrice": false,
.
.
.
.
"currentRetailStorePrice": "29.99",
"isoCurrencyCode": "ZAR",
"isPromotionalPrice": false,
|
whereas what I actually want is this:
Code: |
{
"response": {
"promotions": [
{
"currentRetailStorePrice": "29.99",
"isoCurrencyCode": "ZAR",
"isPromotionalPrice": false,
.
.
.
},
{
"currentRetailStorePrice": "10.99",
"isoCurrencyCode": "ZAR",
"isPromotionalPrice": false
.
.
.
|
How can I add LASTCHILD elements wihtout having to keep track of the subscript value?
TIA |
|
Back to top |
|
 |
mgk |
Posted: Wed Feb 19, 2025 2:36 am Post subject: |
|
|
 Padawan
Joined: 31 Jul 2003 Posts: 1642
|
To see what tree shape you should be creating I would recommend you create a JSON message that represents the output message you are looking to create. Then put that message into a flow that has a trace node that will trace ${Root} and look at the output of the trace. You will see the element types of each of the elements created and then you should be able to replicate this shape in your ESQL. I think you are missing a JSON Array field type, but the trace node will show you for sure.
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 |
|
 |
petervh1 |
Posted: Mon Feb 24, 2025 1:34 am Post subject: |
|
|
Centurion
Joined: 19 Apr 2010 Posts: 135
|
I've tried the following, but I can't create elements in the array:
When I code
Code: |
CREATE FIELD OutputRoot.JSON.Data.response;
CREATE FIELD OutputRoot.JSON.Data.response.promotions IDENTITY (JSON.Array);
SET OutputRoot.JSON.Data.response.promotions.Item[1].currentRetailStorePrice = '1';
SET OutputRoot.JSON.Data.response.promotions.Item[1].isoCurrencyCode = '1';
SET OutputRoot.JSON.Data.response.promotions.Item[2].currentRetailStorePrice = '2';
SET OutputRoot.JSON.Data.response.promotions.Item[2].isoCurrencyCode = '2';
|
I get correct output, and the trace shows:
Code: |
(0x01000000:Object):JSON = ( ['json' : 0x29123b5bb70]
(0x01000000:Object):Data = (
(0x01000000:Object):response = (
(0x01001000:Array):promotions = (
(0x01000000:Object):Item = (
(0x03000000:NameValue):currentRetailStorePrice = '1' (CHARACTER)
(0x03000000:NameValue):isoCurrencyCode = '1' (CHARACTER)
)
(0x01000000:Object):Item = (
(0x03000000:NameValue):currentRetailStorePrice = '2' (CHARACTER)
(0x03000000:NameValue):isoCurrencyCode = '2' (CHARACTER)
)
)
)
)
)
|
When I code:
Code: |
CREATE FIELD OutputRoot.JSON.Data.response;
CREATE FIELD OutputRoot.JSON.Data.response.promotions IDENTITY (JSON.Array);
CREATE LASTCHILD OF OutputRoot.JSON.Data.response.promotions.Item TYPE NameValue Name 'currentRetailStorePrice' VALUE '1';
CREATE LASTCHILD OF OutputRoot.JSON.Data.response.promotions.Item TYPE NameValue Name 'isoCurrencyCode' VALUE '1';
CREATE LASTCHILD OF OutputRoot.JSON.Data.response.promotions.Item TYPE NameValue Name 'currentRetailStorePrice' VALUE '2';
CREATE LASTCHILD OF OutputRoot.JSON.Data.response.promotions.Item TYPE NameValue Name 'isoCurrencyCode' VALUE '2';
|
I get:
Code: |
(0x01000000:Object):JSON = ( ['json' : 0x291242c16f0]
(0x01000000:Object):Data = (
(0x01000000:Object):response = (
(0x01001000:Array):promotions = (
(0x01000000:Object):Item = (
(0x03000000:NameValue):currentRetailStorePrice = '1' (CHARACTER)
(0x03000000:NameValue):isoCurrencyCode = '1' (CHARACTER)
(0x03000000:NameValue):currentRetailStorePrice = '2' (CHARACTER)
(0x03000000:NameValue):isoCurrencyCode = '2' (CHARACTER)
)
)
)
)
)
)
|
which is wrong.
I'm not sure I understand your statement
Quote: |
I think you are missing a JSON Array field type
|
The documentation on how to code the TYPE NameValue is not clear (to me, at least). It just says:
Quote: |
For details of the syntax of the TYPE clause, see Specifying namespaces in the Message property
|
|
|
Back to top |
|
 |
petervh1 |
Posted: Mon Feb 24, 2025 4:42 am Post subject: |
|
|
Centurion
Joined: 19 Apr 2010 Posts: 135
|
|
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
|
|
|
|