Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

jolt transform ( add array elements to a different existing array )

avatar
New Contributor

Hello , can anyone help with a spec to transform this JSON input to the desired output .

 

JSON INPUT

==========

{
"saleStrategy": {
"strategy": "strategy1",
"code": "code1"
},
"saleStrategyCollection": [
{
"id": "2",
"saleStrategy": {
"strategy": "strategy2",
"code": "code2"
}
},
{
"id": "3",
"saleStrategy": {
"strategy": "strategy3",
"code": "code3"
}
}
]
}

== desired output json ==
{
"saleStrategies" : [
{
"strategy" : "strategy1",
"code" : "code1"
},
{
"strategy" : "strategy2",
"code" : "code2"
},
{
"strategy" : "strategy3",
"code" : "code3"
}
]
}

== current spec === // this currently only transforms the initial single saleStrategy object to an array called salesStrategies. Desired output above shows that If there is a saleStrategy array also present in the input JSON , then the spec needs to add the saleStrategy objects within that array to the salesStrategies output array as well . NOTE the transform must not include "id" in the output.

[
{
"operation": "shift",
"spec": {
"saleStrategy": {
"strategy": "saleStrategies[0].strategy",
"code": "saleStrategies[0].code"
}
}
}
]

Any suggestions would be greatly appreciated. Thanks in advance, Rob.

 

1 ACCEPTED SOLUTION

avatar

Hi @rob1 ,

Please try the following transformation:

[
  // first shift will group all strategies under one collection: saleStrategyCollection 
  {
    "operation": "shift",
    "spec": {
      "saleStrategyCollection": "&",
      "saleStrategy": "saleStrategyCollection[].&"
    }
  },
  // 2ed shift will bucket each strategy & code as an array item ([&2]=0,1,2..)
  // under saleStrategies collection and use the same key (&)
  {
    "operation": "shift",
    "spec": {
      "saleStrategyCollection": {
        "*": { // level 2 = 0,1,2..
          "saleStrategy": { //level 1
            "strategy": "saleStrategies[&2].&", //level 0
            "code": "saleStrategies[&2].&"
          }
        }
      }
    }
  }
  ]

If that helps, please accept solution.

Thanks

 

View solution in original post

4 REPLIES 4

avatar
Community Manager

@rob1 Welcome to the Cloudera Community!

I noticed that your post may be related to NiFI. To help you get the best possible solution, I have tagged our NiFi experts @MattWho  and @SAMSAL who may be able to assist you further.

Please keep us updated on your post, and we hope you find a satisfactory solution to your query.


Regards,

Diana Torres,
Community Moderator


Was your question answered? Make sure to mark the answer as the accepted solution.
If you find a reply useful, say thanks by clicking on the thumbs up button.
Learn more about the Cloudera Community:

avatar
New Contributor

Thanks Diana I'll look out for their reply.

avatar

Hi @rob1 ,

Please try the following transformation:

[
  // first shift will group all strategies under one collection: saleStrategyCollection 
  {
    "operation": "shift",
    "spec": {
      "saleStrategyCollection": "&",
      "saleStrategy": "saleStrategyCollection[].&"
    }
  },
  // 2ed shift will bucket each strategy & code as an array item ([&2]=0,1,2..)
  // under saleStrategies collection and use the same key (&)
  {
    "operation": "shift",
    "spec": {
      "saleStrategyCollection": {
        "*": { // level 2 = 0,1,2..
          "saleStrategy": { //level 1
            "strategy": "saleStrategies[&2].&", //level 0
            "code": "saleStrategies[&2].&"
          }
        }
      }
    }
  }
  ]

If that helps, please accept solution.

Thanks

 

avatar
New Contributor

Hi Samsal - that's perfect. Many thanks , Rob 👍