Skip to main content
rob1
Silver ⭐️⭐️
March 8, 2023
Solved

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

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.

 

SAMSAL
SAMSALAnswer
Platinum ⭐️⭐️⭐️
March 9, 2023

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

 

4 replies

DianaTorres
Community Manager
Community Manager
March 8, 2023

@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.

Translate
Regards, Diana Torres, Senior Community Moderator
rob1
rob1Author
Silver ⭐️⭐️
March 9, 2023

Thanks Diana I'll look out for their reply.

Translate
SAMSAL
SAMSALAnswer
Platinum ⭐️⭐️⭐️
March 9, 2023

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

 

Translate
rob1
rob1Author
Silver ⭐️⭐️
March 11, 2023

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

 

Translate