Forum Discussion

vgautam64's avatar
vgautam64
New Contributor III
2 years ago
Solved

Unable to merge the objects of an array as a single object

I have data that looks like this:

I want to achieve the following result:

Please help!

  • Hi vgautam64 , I am unable to attached pipeline. so give you an example- 

    Data: 

    {
        "carList": [
        {
        "color""purple",
        "type""minivan",
        "registration""2017-01-03",
        "capacity"7
      },
      {
        "color""red",
        "type""station wagon",
        "registration""2018-01-03",
        "capacity"5
      }, {
        "color""black",
        "type""toyota",
        "registration""2017-01-03",
        "capacity"5
      },
      {
        "color""blue",
        "type""Maruti",
        "registration""2018-01-03",
        "capacity"5
      }
    ]
    }
    1. use mapper for adding index : $carList.map((val,index)=> val.mapKeys((v,k)=> k+(index+1)))    : $carList (target path of mapper)
    2. use another mapper to merge in single array obejct : sl.ensureArray($carList.reduce((curr,acc)=>curr.extend(acc),{} ))
  • Hey vgautam64,

    Try the following expression:

    $group.reduce((acc,curr,ind)=> acc.extend(curr.mapKeys((val,key)=>key+ind)),{})

    Regards,

    Bojan

9 Replies

  • This is a pretty gross expression, but you could put this in a mapper to achieve the result, since the output is an object rather than array, the order likely doesn't really matter in this case:

    $group.reduce((acc, val, ind) => acc.extend(...val.entries().map((k) => {[k[0] + (ind+1)]: k[1]})), {})

    • vgautam64's avatar
      vgautam64
      New Contributor III

      Thank you ddellsperger. The expression is not gross at all and it looks great. It looks like it could get me to the result but I honestly did not try it because I would prefer the keys to be in order since they have to be written as columns to an error report. I really appreciate your help though 🙂

  • Supratim's avatar
    Supratim
    Contributor III

    Hi vgautam64 , I am unable to attached pipeline. so give you an example- 

    Data: 

    {
        "carList": [
        {
        "color""purple",
        "type""minivan",
        "registration""2017-01-03",
        "capacity"7
      },
      {
        "color""red",
        "type""station wagon",
        "registration""2018-01-03",
        "capacity"5
      }, {
        "color""black",
        "type""toyota",
        "registration""2017-01-03",
        "capacity"5
      },
      {
        "color""blue",
        "type""Maruti",
        "registration""2018-01-03",
        "capacity"5
      }
    ]
    }
    1. use mapper for adding index : $carList.map((val,index)=> val.mapKeys((v,k)=> k+(index+1)))    : $carList (target path of mapper)
    2. use another mapper to merge in single array obejct : sl.ensureArray($carList.reduce((curr,acc)=>curr.extend(acc),{} ))
    • vgautam64's avatar
      vgautam64
      New Contributor III

      Thanks Supratim. This expression gets me to the desired result as well but I realized I don't need the Snaplogic's ensureArray function.

  • bojanvelevski's avatar
    bojanvelevski
    Valued Contributor

    Hey vgautam64,

    Try the following expression:

    $group.reduce((acc,curr,ind)=> acc.extend(curr.mapKeys((val,key)=>key+ind)),{})

    Regards,

    Bojan

  • vgautam64's avatar
    vgautam64
    New Contributor III

    Hi bojanvelevski. This is such an elegant expression to achieve the task. I really appreciate the help. Thanks a lot!

  • vgautam64's avatar
    vgautam64
    New Contributor III

    Note that this is just an example and the number of objects in the group array is not fixed.

  • dmiller's avatar
    dmiller
    Former Employee

    Thanks for bumping this up. I haven't had a chance to take a look into it yet, but let me see what I can do or who I can find to help (unfortunately, tstack is no longer around).