App Connect

 View Only
  • 1.  Deleting full array from the message tree

    IBM Champion
    Posted Tue May 10, 2022 09:29 AM
      |   view attached
    Is there a proper way to delete the contents of an array from the message tree while still keeping the array field?
    Let's assume that copying everything except the array from input to output is not an option.

    What is the most efficient way to go from this
    {
    	"field1": "content1",
    	"field2": "content2",
    	"array1": ["arrayContent1", "arrayContent2", "arrayContent3"],
    	"field3": "content3"
    }

    To this
    {
    	"field1": "content1",
    	"field2": "content2",
    	"array1": "",
    	"field3": "content3"
    }​

    I've tried setting it to null, deleting the field and creating a new field over it all to now avail.
    Looping over the array and deleting the contents seems to be the easiest way except for completly deleting it and then creating it again as a sibling of field2.

    I've attached a PI with some test code I tried.

    I'm curious if there is a proper way of doing this that we have missed.

    ------------------------------
    Regards
    Matthias Blomme
    ------------------------------

    Attachment(s)

    zip
    deleteArrayContents.zip   2 KB 1 version


  • 2.  RE: Deleting full array from the message tree

    Posted Tue May 10, 2022 03:34 PM
      |   view attached
    @Matthias Blomme​Sorry it took so long to get back to you on this, I didn't see it right away.

    I looked at your examples and I'm providing an updated PI for you to look at. I wouldn't use the loop example you had, because I really, REALLY, don't like using CARDINALITY. It's a very expensive operation and that only gets worse as the size of the array increases.

    The fastest way is to delete the array and recreate it. You were close on your attempt, but you were missing the IDENTITY clause, which would make it an array, rather than an empty element. I have provided a different looping construct for you to consider, if you still want to go that route.

    I also do recommend creating the DOMAIN in the Environment space, if you want to keep the formatting, especially important for XML with Attributes, but I do that for all DOMAINS to make sure I'm not losing anything.

    --New Test - loop
    WHILE EXISTS(rTest3.array1.Item[]) DO
    	DELETE FIELD rTest3.array1.Item[<];
    END WHILE;
    		
    --New Test - fastest	
    SET rTest4.array1 = null;
    CREATE FIELD rTest4.array1 IDENTITY(JSON.Array) array1;
    ​


    ------------------------------
    James E. (Jim) Berube
    Technical Account Manager (TAM) - Specialist, IBM Expertise Connect, IBM Expert Labs
    IBM Software
    ------------------------------

    Attachment(s)



  • 3.  RE: Deleting full array from the message tree

    Posted Tue May 10, 2022 03:43 PM
    @Matthias Blomme,

    Something I didn't notice until after I sent my reply. If the location of the empty array, in between the fields, is important, then the loop I showed you will keep the structure in tact. If deleting and recreating, the new array is created at the end, after all the elements.
    Output:


    ------------------------------
    James E. (Jim) Berube
    Technical Account Manager (TAM) - Specialist, IBM Expertise Connect, IBM Expert Labs
    IBM Software
    ------------------------------



  • 4.  RE: Deleting full array from the message tree

    IBM Champion
    Posted Wed May 11, 2022 08:59 AM
    Hi Jim

    The location is indeed important.
    Your solution with exists is indeed more elegant, but I've had some weird experiences with exists if you use reference to go over your data.
    So I'm a bit hesitant using it. But is it by far more elegant.
    Thanks for the fast reply!

    ------------------------------
    Regards
    Matthias Blomme
    ------------------------------