cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Json splitter for multiple array objects

neelamanikandan
New Contributor II

I have a Json input that contains multiple array objects within it. Json sample given below. I'm trying to flatten this data into table rows, I use two json splitter to achieve this.

First Json splitter with Split path $ServiceCompanyArray and then a mapper to map the split value and then another Json splitter with split path $ServiceCompanyDetailArray and then another mapper to Writer.

Note: this is an xample Json, the actual Json could have multiple array fields, so should we always use N number of json splitters.

{
    "Vehicle_Id""_Id4",
    "InceptionDt""2023-08-03",
    "ServiceCompanyArray": [
        {
            "@_Id""_Id5",
            "$""ServiceCompany45"
        },
        {
            "@_Id""_Id6",
            "$""ServiceCompany47"
        }
    ],
    "ServiceCompanyDetailArray": [
        {
            "ServiceCompanyAddressLine1""ServiceCompanyAddressLine150",
            "ServiceCompanyPhone""ServiceCompanyPhone26",
            "ServiceCompanyPostalCode""ServiceCompanyPostalCode2"
        },
        {
            "ServiceCompanyAddressLine1""ServiceCompanyAddressLine163",
            "ServiceCompanyPhone""ServiceCompanyPhone9",
            "ServiceCompanyPostalCode""ServiceCompanyPostalCode15"
        }
    ]
}

6 REPLIES 6

alchemiz
Contributor III

Hi @neelamanikandan ,

Good day, see if this will work place the expression in a mapper 

sl.ensureArray($ServiceCompanyArray).map(c=> sl.ensureArray($ServiceCompanyDetailArray).map(d=> {'Vehicle_Id': $Vehicle_Id, 'InceptionDt': $InceptionDt}.merge(c.merge(d)))).reduce((a,b)=> a.concat(b),[])

alchemiz_0-1691266507400.png

Thanks,

EmEm

This works Thank You! 

But this will also be a too much of coding when we have multiple attributes, and multiple arrays. Anyways thanks for this solution, we can take it from here.