'JOLT transformation modify overwrite - replace element value in array

I am working on JOLT library to perform a change to the json values.

For key-value items I found a solution using

"operation": "modify-overwrite-beta"

But when it comes to edit values inside the arrays I encounter problems.

Let's have for example this JSON:

{
  "parentModule": [
    {
      "childModule": {
        "arrayModule": [
          "KK",
          "VV"
        ]
      }
    }
  ]
}

SPEC I am using

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "parentModule": {
        "*": {
          "childModule": {
            "arrayModule": [
              "TT",
              "RR"
            ]
          }
        }
      }
    }
  }
]

The result I want is that the array is totally override , but currently it is replacing only the first value.

Result expected:

{
 "parentModule": [
    {
      "childModule": {
        "arrayModule": [
          "TT",
          "RR"
        ]
      }
    }
  ]
}

Is there any way to:

  1. completely override the array?
  2. change values conditionally, for example if TT => change to AB, else if RR than write BB ?

Thanks



Solution 1:[1]

You can use shift transformations along with # operators in order to represent the fixed element values for the new lists to be created.

For the first case( if we have "arrayModule": ["KK", "VV"] for the input ) :

 [
   {
     "operation": "shift",
     "spec": {
       "parentModule": {
         "*": {
           "childModule": {
             "arrayModule": {
               "#TT": "&4[&3].&2.&1[]",
               "#RR": "&4[&3].&2.&1[]"
             }
           }
         }
       }
     }
   }
]

the demo1 :

enter image description here

And for the second ( if we have "arrayModule": ["TT", "RR"] for the input ) :

 [
   {
     "operation": "shift",
     "spec": {
       "parentModule": {
         "*": {
           "childModule": {
             "arrayModule": {
               "*": {
                 "TT": { "#AB": "&6[&5].&4.&3" },
                 "RR": { "#BB": "&6[&5].&4.&3" }
               }
             }
           }
         }
       }
     }
   }
]

the demo2 :

enter image description here

while setting proper ampersand levels to reach the desired key names at several levels respectively.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Barbaros Özhan