|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
 |
|
Amending / Adding to a JSON array |
« View previous topic :: View next topic » |
Author |
Message
|
lonesheep |
Posted: Fri Oct 12, 2018 5:21 am Post subject: Amending / Adding to a JSON array |
|
|
Novice
Joined: 12 Sep 2017 Posts: 21
|
Given the following JSON strcuture, if I wanted to add another address to 'fred', any idea how I do it?
Level 1 {Array}
Firstname fred
Level 2
Address {Array}
Level 3
Building No 1
Street High Street
Level 3
Building No 2
Street Station Road
Firstname bob
Level 2
Address {Array}
Level 3
Building No 1
Street High Street
I'm assuming I need to get a reference to the JSON Array at Address {Level 2}, but I have no idea how to do this? I've used the following to check for the existance of the block, but I can't see to amend it.
IF EXISTS(SELECT ITEM NAME from refOutNamesArray.Item[] as NAME where NAME.Firstname = 'fred') = TRUE THEN
-- Add an address..
END IF;
To add the address I tried this...
DECLARE refName REFERENCE TO OutputRoot;
SET refName = THE (SELECT ITEM NAME from refOutNamesArray.Item[] as NAME where NAME.Firstname = 'fred';
DECLARE countAddress INT CARDINALITY(refName.Address.Item[]);
SET (refName.Address.Item[countAddress+1].Street = 'Another One';
Alias, not good. I can't see to find any information on how to do this online. Any ideas? |
|
Back to top |
|
 |
Vitor |
Posted: Fri Oct 12, 2018 5:57 am Post subject: Re: Amending / Adding to a JSON array |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
lonesheep wrote: |
I'm assuming I need to get a reference to the JSON Array at Address {Level 2}, but I have no idea how to do this? I've used the following to check for the existance of the block, but I can't see to amend it.
IF EXISTS(SELECT ITEM NAME from refOutNamesArray.Item[] as NAME where NAME.Firstname = 'fred') = TRUE THEN
-- Add an address..
END IF;
|
If it was me, I'd do something like:
Code: |
MOVE refName FIRSTCHILD refOutNamesArray NAME 'NAME';
WHILE LASTMOVE(refName) DO
IF FIELDVALUE(refName) = 'fred' THEN;
MOVE refAddress FIRSTCHILD NAME 'Address';
CREATE NEXTSIBLING OF refAddress AS refAddress;
SET refAddress.<etc, etc>
END IF;
MOVE refName NEXTSIBLING;
END DO;
|
All code untested, posted as-is with no liability express or implied accepted for any loss or damage howsoever caused and on the understanding I've had so very much coffee this morning.
lonesheep wrote: |
To add the address I tried this...
DECLARE refName REFERENCE TO OutputRoot;
SET refName = THE (SELECT ITEM NAME from refOutNamesArray.Item[] as NAME where NAME.Firstname = 'fred';
DECLARE countAddress INT CARDINALITY(refName.Address.Item[]);
SET (refName.Address.Item[countAddress+1].Street = 'Another One';
|
Never use CARDINALITY. _________________ Honesty is the best policy.
Insanity is the best defence. |
|
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
|
|
|
|