Jolt - Conditional update LHS value

Viewed 146

I basically have 2 different condition which can impact the value of a key in the output.
I have following input:

{
  "state": "PRESENT",
  "sample": {
    "type": "OP"
  }
}

I have following spec:

[
  {
    "operation": "shift",
    "spec": {
      "state": {
        "PRESENT": {
          "#ANSWER1": "answer"
        },
        "*": {
          "$": "answer"
        }
      },
      "sample":{
        "type":{
          "OP":{
            "#ANSWER2": "answer"
          }
        }
      }
    }
  }
]

Now if you see that both the conditions are true, the final output is as following. Both the values come in form of an array

{
  "answer" : [ "ANSWER1", "ANSWER2" ]
}

But I am looking for an output based on the second condition. Basically override the existing value in "answer" attribute:

{
  "answer" : "ANSWER2"
}
1 Answers

You can add one more shift transformation

{
  "operation": "shift",
  "spec": {
    "*": { "1": { "@": "&2" } }
  }
}

to the existing specification in order to pick the second(the element with the index 1) element.

If picking the left element of the array was the case, then cardinality transformation might be preferred to use

{
  "operation": "cardinality",
  "spec": {
    "*": "ONE"
  }
}
Related