Home > OS >  Conditional replacement of values in JOLT
Conditional replacement of values in JOLT

Time:07-15

I try to replace a value conditionally in JOLT. The first step is to check whether the key "links" exists. If this key exists, it should also be checked whether the key "type" has the value "null". If both conditions are true, the value of the key "type" shall be changed to "buttons".

In the example, this condition only applies to flow1.

Sample Input:

{
  "flows": {
    "flow1": { // <--- Only this object fulfills both conditions ("link" exist, "type" = null)
      "link": "linkname",
      "type": "null" // <--- This value is to be replaced
    },
    "flow2": {
      "link": "linkname",
      "type": "text"
    },
    "flow3": {
      "type": "null"
    }
  }
}

Expected Output:

{
  "flows": {
    "flow1": {
      "link": "linkname",
      "type": "button" // <-- replaced value
    },
    "flow2": {
      "link": "linkname",
      "type": "text"
    },
    "flow3": {
      "type": "null"
    }
  }
}

My Spec:

[
  {
    "operation": "shift",
    "spec": {
      "flows": {
        "*": {
          "link": {
            "@(1,type)": {
              "null": {
                "#button": "&5.&4.type"
              }
            },
            "*": "&3.&2.&"
          },
          "*": "&2.&1.&"
        }
      }
    }
  }
]

CodePudding user response:

You're on the right track. Just apply some little tweaks within the current shift transformation, and then apply cardinality transformation in order to pick the first components of the generated arrays for the type attribute such as

[
  {
    "operation": "shift",
    "spec": {
      "flows": {
        "*": {
          "link": {
            "@(1,type)": {
              "null": {
                "#button": "&5.&4.type"
              },
              "*": {
                "@(3,type)": "&5.&4.type"
              }
            },
            "@": "&3.&2.&"
          },
          "*": "&2.&1.&"
        }
      }
    }
   },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": {
          "type": "ONE"
        }
      }
    }
  }
]

the demo on the site enter image description here

  • Related