Forum Discussion

neelamanikandan's avatar
neelamanikandan
New Contributor II
2 years ago

Json splitter for multiple array objects

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

  • Hi neelamanikandan, thank you for joining the SnapLogic Community within the last few weeks and for sharing how you split multiple array objects. I just want to double check, did you have a question about how you split the JSON arrays or were you just sharing a solution?

    • neelamanikandan's avatar
      neelamanikandan
      New Contributor II

      Hi RogerSramkoski  It was a question, in the above example I had 2 (Servicecompany and Servicecompanydetail) fields that had values in array, so I have used 2 JSON splitters in the pipeline. But if my input JSON contains multiple(eg. 10) array fields should I always use multiple (10 JSON splitters) in the pipeline? Is there alternate Snap that can flatten all the Array elements at one go, or is there an alternative approach to use the JSON splitter to flatten all arrays

    • neelamanikandan's avatar
      neelamanikandan
      New Contributor II
      Vehicle_IdInceptionDt@_Id$ServiceCompanyAddressLine1ServiceCompanyPhoneServiceCompanyPostalCode
      _Id408/03/2023_Id5ServiceCompany45ServiceCompanyAddressLine150ServiceCompanyPhone26ServiceCompanyPostalCode2
      _Id408/03/2023_Id5ServiceCompany45ServiceCompanyAddressLine163ServiceCompanyPhone9ServiceCompanyPostalCode15
      _Id408/03/2023_Id6ServiceCompany47ServiceCompanyAddressLine150ServiceCompanyPhone26ServiceCompanyPostalCode2
      _Id408/03/2023_Id6ServiceCompany47ServiceCompanyAddressLine163ServiceCompanyPhone9ServiceCompanyPostalCode15
  • alchemiz's avatar
    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),[])

    Thanks,

    EmEm

    • neelamanikandan's avatar
      neelamanikandan
      New Contributor II

      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.