bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.54k stars 328 forks source link

How to remove an element from root node based on a condition satisfaction of an another element in Jolt? #1202

Open lonewolfsachin opened 1 year ago

lonewolfsachin commented 1 year ago

Suppose I have an input JSON as below

{ "title": "title1", "addBooleanFields": "true", "variables": [ { "props": {}, "question": "question1", "value": "textbox" }, { "props": {}, "question": "question2", "value": "radio" } ], "formattedVariables": [ { "props": {}, "question": "question3", "value": "textbox", "isFormat": "true" }, { "props": {}, "question": "question4", "value": "radio", "isFormat": "true" } ] }

I want to remove the "variables" array if I find that the element "addBooleanFields" is having value as "true".

I tried with the below spec but I am not getting successful result for a condition based element removal in JOLT

[ { "operation": "remove", "spec": { "addBooleanFields": { "@(2,variables)": "" } } } ]

Kindly help.

The full requirement is that I will get the input json like below

{ "title" : "title1", "variables": [ { "props": {}, "question": "question1", "value": "textbox" }, { "props": {}, "question": "question2", "value": "radio" }, { "props": {}, "question": "question3", "value": "textbox" }, { "props": {}, "question": "question4", "value": "radio" ] }

and if I find that the "title" field is having value like "title1", then I will add a new field "addBooleanFields" under root node having value as "true". then in the next operation, I will check if I am having value of "addBooleanFields" as true and if yes, then I need to traverse through the "variables" array and see if "question" field is having value "question3" or "question4" and if the condition matches then I need to add a new element "isFormat" with value "true" for object of "question3" and "question4" .

So the desired output is as below

{ "title" : "title1", "addBooleanFields": "true", "variables": [ { "props": {}, "question": "question1", "value": "textbox" }, { "props": {}, "question": "question2", "value": "radio" }, { "props": {}, "question": "question3", "value": "textbox", "isFormat" : "true" }, { "props": {}, "question": "question4", "value": "radio", "isFormat" : "true" } ] }

Spec I wrote [ { "operation": "shift", "spec": { "title": [ "title", "titleTransform" ], "": "&" } }, { "operation": "shift", "spec": { "": "&", "titleTransform": { "title1": { "#true": "addBooleanFields" } } } }, { "operation": "shift", "spec": { "": "&", "addBooleanFields": { "true": { "@(2,variables)": "variablesTransform" } } } }, { "operation": "shift", "spec": { "": "&", "variablesTransform": { "": { "question": { "question3": { "$": "variablesTransform.[&2].question", "#true": "variablesTransform[&3].isFormat" }, "question4": { "$": "variablesTransform.[&2].question", "#true": "variablesTransform[&3].isFormat" }, "@": "variablesTransform[&2].question" }, "props": "variablesTransform[&1].props", "value": "variablesTransform[&1].value" } } } }, { "operation": "remove", "spec": { "addBooleanFields": { "@(2,variables)": "" } } }, { "operation": "shift", "spec": { "": "&", "variablesTransform": "variables" } } ]

Please let me know if there is any alternative way to achieve the desired output