2

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

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
Nadir Bertolasi
  • 527
  • 6
  • 17

1 Answers1

2

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.

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
  • Thankyou for your answer @Barbaros, can you pls explain what this sintax refers to "&6[&5].&4.&3" to understand it better – Nadir Bertolasi Mar 10 '22 at 16:48
  • Hi @NadirBertolasi &6 stands for traversing 6 times `{` in order to grab key name `parentModule`, &4 for `childModule`, &3 for `arrayModule`, and `[&5]` represents the indexes under `parentModule` object. You're welcome. – Barbaros Özhan Mar 10 '22 at 16:57