jsonetlapache-nifijolt

JOLT shift transformation to filter values in array


I want to use a JOLT transformation to do two things:

Input:

{
  "isError": false,
  "isValid": true,
  "myarray": [
    {
      "p_0001": "1",
      "p_0002": "1",
      "p_0003": "1",
      "p_0004": "1",
      "v_518": "0,214506186",
      "lfdn": 89709
    },
    {
      "p_0001": "2",
      "p_0002": "1",
      "p_0003": "1",
      "v_518": "0,3823236",
      "lfdn": 89710
    },
    {
      "p_0001": "3",
      "p_0002": "1",
      "p_0003": "1",
      "lfdn": 89711
    }
  ],
  "errorMessage": null,
  "exceptionMessage": null,
  "innerExceptionMessage": null
}

Desired output:

{
  "isError": false,
  "isValid": true,
  "myarray": [
    {
      "v_518": "0,214506186",
      "lfdn": 89709
    },
    {
      "v_518": "0,3823236",
      "lfdn": 89710
    }
  ],
  "errorMessage": null,
  "exceptionMessage": null,
  "innerExceptionMessage": null
}

What I tried so far, but doesn't work as intended:

[
  {
    "operation": "shift",
    "spec": {
      "isError": "isError",
      "isValid": "isValid",
      "myarray": {
        // loop thru all the elements in value array
        "*": {
          "v_518": {
            // if the value "v_518" exists
            // grab the whole object and write it out to
            // a v_518_array array.
            "@(1,v_518)": "v_518_array",
            "@(1,lfdn)": "v_518_array"
          }
        }
      },
      "errorMessage": "errorMessage",
      "exceptionMessage": "exceptionMessage",
      "innerExceptionMessage": "innerExceptionMessage"
    }
  }
]

I tried working with the examples in http://jolt-demo.appspot.com/#andrewkcarter2 but I couldn't figure out how to do it.


Solution

  • I was able to solve my issue. This answer was the hint I needed to get the ball rolling: https://stackoverflow.com/a/38154541/1561441

    The key is referencing the array you are currently transforming via "value" = "array[&1].value".

    In my mind I spent way too much time on this issue. Does anyone know of a good documentation for the Jolt syntax? I couldn't find a satisfactory one by googling myself.

      [
          {
            "operation": "shift",
            "spec": {
              "isError": "isError",
              "isValid": "isValid",
              "myarray": {
                // loop thru all the elements in value array
                "*": {
                  "v_518": {
                    // if the value "v_518" exists
                    // grab the whole object and write it out to
                    // a v_518_array array.
                    "@1": "v_518_array"
                  }
                }
              },
              "errorMessage": "errorMessage",
              "exceptionMessage": "exceptionMessage",
              "innerExceptionMessage": "innerExceptionMessage"
            }
          },
          {
            "operation": "shift",
            //Transform array: https://stackoverflow.com/questions/37865871/how-do-i-transform-an-array-using-jolt
            "spec": {
              "v_518_array": {
                // loop thru all the elements in value array
                "*": {
                  "v_518": "v_518_array[&1].v_518",
                  "lfdn": "v_518_array[&1].lfdn"
                }
              }
            }
          }
        ]
    

    Here's a slightly better solution:

    [
      {
        "operation": "shift",
        "spec": {
          "isError": "isError",
          "isValid": "isValid",
          "myarray": {
            // loop thru all the elements in value array
            "*": {
              "v_518": {
                // if the value "v_518" exists
                // grab the whole object and write it out to
                // a v_518_array array.
                "@1": "v_518_array"
              }
            }
          },
          "errorMessage": "errorMessage",
          "exceptionMessage": "exceptionMessage",
          "innerExceptionMessage": "innerExceptionMessage"
        }
      },
      {
        "operation": "shift",
        //Transform array: https://stackoverflow.com/questions/37865871/how-do-i-transform-an-array-using-jolt
        "spec": {
          "v_518_array": {
            // loop thru all the elements in value array
            "*": {
              "v_518": "&2[&1].v_518", //notice the generic shorthand here
              "lfdn": "&2[&1].lfdn"
            }
          }
        }
      }
    ]