skip to Main Content

Is it possible to do more than one condition on a JOLT transformation to create or’s or and’s?

I found some documentation on If Else but haven’t been able to apply it to what I’m trying for. This is an if else on a single element, I need it with multiple or’s. For instance I have the following json:

[
  {
    "alpha": "a",
    "beta": "b",
    "omega": "o",
    "exists": true,
    "description": "goodbye world"
  },
  {
    "alpha": "1",
    "beta": "2",
    "omega": "3",
    "exists": false,
    "description": "hello world"
  },
  {
    "alpha": "z",
    "beta": "x",
    "omega": "y",
    "exists": true,
    "description": "hello mars"
  }
]

I need to figure out a JOLT that can look at the entire array and say "if Exists is True or description is hello world" So it outputs:

[
  {
    "alpha": "a",
    "beta": "b",
    "omega": "o",
    "exists": true,
    "description": "goodbye world"
  },
  {
    "alpha": "1",
    "beta": "2",
    "omega": "3",
    "exists": false,
    "description": "hello world"
  },
  {
    "alpha": "z",
    "beta": "x",
    "omega": "y",
    "exists": true,
    "description": "hello mars"
  }
]

Jolt to find hello world:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "description": {
          "hello world": null,
          "*": {
            "@2": "&1"
          }
        }
      }
    }
  }
]

I know how jolt for for the if exists and the filter or description piece but how do I put them together to work in unison like an or clause such as:
if(("exists" == "true") | ("description" == "hello world"))

2

Answers


  1. You can generate a new attribute by concatenating those values(true and hello world), and then check out for the existence by your conditional checking method such as

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "ed": "=concat(@(1,exists),@(1,description))"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "ed": {
              "true*|*hello world": { // | stands for OR
                                      // check out the value of "ed" whether
                                      // starts with "true" OR ends with "hello world"
                "@2": "" // replicate the whole value from the "ed"'s level
                         // without any object key(eg. "") 
              }
            }
          }
        }
      },
      { // get rd of the lately generated attribute
        "operation": "remove",
        "spec": {
          "*": {
            "ed": ""
          }
        }
      }
    ]
    
    Login or Signup to reply.
  2. What do you think of the below solution which I seem to make it work with one shift unless I’m missing something.

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "exists": {
              "true": {
                "@(2)": "[]"
              },
              "*": {
                "@(2,description)": {
                  "hello world": {
                    "@(4)": "[]"
                  }
                }
              } /**/
            }
          }
        }
     }
    ]
    

    Thanks.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search