cancel
Showing results for 
Search instead for 
Did you mean: 

Merging 2 arrays into single array

nickhumble
New Contributor II

I have the below JSON output from a Union (below is just a subset of the results), which combines the output of 2 SOAP calls into the 2 arrays.

I'm trying to merge NCTimeTypes array with CHTimeTypes array to create a single array "TimeTypes" that contains the contents of both. 

e.g.  Transform this:

[
    {
        "NCTimeTypes": [
            {
                "Code": "NCDPCS11",
                "Description": "DP&CS Account Planning",
                "TimeTypeId": "f51e464f-191e-4a96-a269-043449ace7f2",
                "TransactionType": "FEES",
                "isActive": "1",
                "isChargeable": "0"
            },
            {
                "Code": "NCDPCS18",
                "Description": "DP&CS Administration",
                "TimeTypeId": "fcc13d0f-caf4-44e4-8ec7-a6e6161352a7",
                "TransactionType": "FEES",
                "isActive": "1",
                "isChargeable": "0"
            }
        ]
    },
    {
        "CHTimeTypes": [
            {
                "Code": "ADMINISTRATION",
                "Description": "Administration",
                "TimeTypeId": "72908315-3e54-40ae-b656-bf8b59c3272e",
                "TransactionType": "FEES",
                "isActive": "1",
                "isChargeable": "1"
            },
            {
                "Code": "ADVOCACY",
                "Description": "Advocacy",
                "TimeTypeId": "6b997021-60b0-4dde-a19b-f119939617c3",
                "TransactionType": "FEES",
                "isActive": "1",
                "isChargeable": "1"
            }
        ]
    }
]

To this:

[
    {
        "TimeTypes": [
            {
                "Code": "NCDPCS11",
                "Description": "DP&CS Account Planning",
                "TimeTypeId": "f51e464f-191e-4a96-a269-043449ace7f2",
                "TransactionType": "FEES",
                "isActive": "1",
                "isChargeable": "0"
            },
            {
                "Code": "NCDPCS18",
                "Description": "DP&CS Administration",
                "TimeTypeId": "fcc13d0f-caf4-44e4-8ec7-a6e6161352a7",
                "TransactionType": "FEES",
                "isActive": "1",
                "isChargeable": "0"
            },
            {
                "Code": "ADMINISTRATION",
                "Description": "Administration",
                "TimeTypeId": "72908315-3e54-40ae-b656-bf8b59c3272e",
                "TransactionType": "FEES",
                "isActive": "1",
                "isChargeable": "1"
            },
            {
                "Code": "ADVOCACY",
                "Description": "Advocacy",
                "TimeTypeId": "6b997021-60b0-4dde-a19b-f119939617c3",
                "TransactionType": "FEES",
                "isActive": "1",
                "isChargeable": "1"
            }
        ]
    }
]

I've tried various approaches, the most appropriate feels like array.concat but i can't get it to work.

Can anyone help?

1 ACCEPTED SOLUTION

koryknick
Employee
Employee

@nickhumble - my apologies - I missed your original response to my question.

If there are two input documents that you want to combine to a single document, I would recommend using the Gate snap in this case.  Then follow with a Mapper snap with the following expression:

koryknick_0-1702060933102.png

The jsonPath is a powerful function that allows you to rip through a JSON structure looking for specific sub-elements easily and return the results as an array.  This specific syntax tells jsonPath to return all elements buried under the top three arrays. 

Note that the Gate snap has some considerations when processing larger volumes of data, but for this case, it works very nicely.  Also, the jsonPath documentation has some external links for additional details of how you can work with it along with some good examples.

Hope this helps!

 

View solution in original post

5 REPLIES 5

koryknick
Employee
Employee

@nickhumble - the input you have listed - is that 2 input documents, or one field value?

nickhumble
New Contributor II

It's 2 input documents, thanks.

nickhumble
New Contributor II

@koryknick any ideas?  Thanks!

koryknick
Employee
Employee

@nickhumble - my apologies - I missed your original response to my question.

If there are two input documents that you want to combine to a single document, I would recommend using the Gate snap in this case.  Then follow with a Mapper snap with the following expression:

koryknick_0-1702060933102.png

The jsonPath is a powerful function that allows you to rip through a JSON structure looking for specific sub-elements easily and return the results as an array.  This specific syntax tells jsonPath to return all elements buried under the top three arrays. 

Note that the Gate snap has some considerations when processing larger volumes of data, but for this case, it works very nicely.  Also, the jsonPath documentation has some external links for additional details of how you can work with it along with some good examples.

Hope this helps!