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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thankyou for your answer @Barbaros, can you pls explain what this sintax refers to "&6[&5].&4.&3" to understand it better
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.