Arnold Joseph
Arnold Joseph

Reputation: 545

Jolt Transform If Else Condition on keys?

Any way we can use if else conditions in Jolt Transform :

Input :

{
  "treasure": [
    {
      "aname": "FOO",
      "bname": "BAR"
    }
  ]
}

Output :

If aname is "FOO" then change to fname else let it be to aname.

{
  "fname" : "FOO",
  "sname" : "BAR"
}

Upvotes: 1

Views: 5033

Answers (3)

Raymond Choi
Raymond Choi

Reputation: 1271

You may consider another library Josson.

https://github.com/octomix/josson

Deserialization

Josson josson = Josson.fromJsonString(
    "{" +
    "  \"treasure\": [" +
    "    {" +
    "      \"aname\": \"FOO\"," +
    "      \"bname\": \"BAR\"" +
    "    }" +
    "  ]" +
    "}");
    

Transformation

JsonNode node = josson.getNode(
    "treasure" +
    ".entries()" +
    ".map(if([key='aname'], if([value='FOO'],'fname',key), 'sname')::value)" +
    ".mergeObjects()");
System.out.println(node.toPrettyString());

Output

{
  "fname" : "FOO",
  "sname" : "BAR"
}

Upvotes: 0

Arnold Joseph
Arnold Joseph

Reputation: 545

Jolt Spec for If - Else:

[
  {
    "operation": "shift",
    "spec": {
      "treasure": {
        "*": {
          "aname": {
            "FOO": {
              "@(2,aname)": "aname"
            },
            "*": {
              "@(2,aname)": "gname"
            }
          },
          "bname": {
            "BAR": {
              "@(2,bname)": "bname"
            },
            "*": {
              "@(2,bname)": "gname"
            }
          }
        }
      }
    }
  }
]

Upvotes: 2

Colin Heyl
Colin Heyl

Reputation: 31

Yep, I can confirm that that answer gives the correct output but so does:

[
  {
    "operation": "shift",
    "spec": {
      "treasure": {
        "*": {
          "aname": "fname",
          "bname": "sname"
        }
      }
    }
  }
]

When you say you're after if/else, do you mean that you want either "aname" or "bname" to feed into "fname"?

Currently, your solution is just a direct mapping of "aname" > "fname" and "bname" > "sname".

Upvotes: 0

Related Questions