Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]

Support Questions

Find answers, ask questions, and share your expertise
Announcements
Share your experience with Cloudera on G2 and get a $25 Amazon Gift card.
Hi, I'm CLEO! Something exciting is coming to the Community. Stay Tuned!

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

avatar
Visitor

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
Super Guru

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,
Senior 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
Visitor

Thanks Diana I'll look out for their reply.

avatar
Super Guru

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
Visitor

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