Support Questions

Find answers, ask questions, and share your expertise

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

avatar
New Contributor

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

JSON INPUT

==========

{
"courseId": "xxx"
}

== desired output json ==

[

{

"courseId": "xxx",
"courseSubCategory": 1

},

{

"courseId": "xxx",
"courseSubCategory": 2

},

{

"courseId": "xxx",
"courseSubCategory": 3

}

]

2 REPLIES 2

avatar
Super Guru

Hi @fisblack ,

Welcome to the community.

Your transformation is kind of odd. Usually transformation is driven by existing values or structure  even when generating new fields with defaults. I have never seen a case where newly generated values drive transformation of existing ones. I assume you are getting the list of courseSubCategory from somewhere and the  script is rather dynamic utilizing some expression language & attributes. if you provide some context to how the list is coming about and what are you trying to achieve , maybe we can provide you with more accurate solution.

There are multiple ways of solving your problem as is - assuming you always want to replicate an existing field(s) against  newly generated 3 elements array with default  values of 1,2&3- , here is one of them:

[
  {
    "operation": "shift",
    "spec": {
      "*": "existing.&",
      "#1|#2|#3": "courseSubCategory"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "courseSubCategory": {
        "*": {
          "@(2,existing)": "[&1]",
          "@": "[&1].courseSubCategory"
        }
      }
    }
  }
]

If you want to make this more dynamic in terms of number of "courseSubCatgory", you can construct the array assignment string "#1|#2|#3" in some upstream processors and store the value as flow attribute (let says its called the same) , then you can make it more dynamic by using the flowfile attribute where the assignment of the courseSubCategory in the first shift will look like this:

"${courseSubCategory}": "courseSubCategory"

Hope that helps. If it does help, please accept the solution.

Thanks

 

 

 

avatar
Community Manager

@fisblack Has the reply helped resolve your issue? If so, please mark the appropriate reply as the solution, as it will make it easier for others to find the answer in the future.  Thanks.


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: