简体   繁体   中英

JOLT shift transformation to filter values in array

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

  • filter the elements in the array called myarray so that only elements remain which have a "v_518" attribute
  • filter out all attributes of the remaining elements except for "v_518" and "lfdn"

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.

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"
        }
      }
    }
  }
]

Still there's a briefer, and more dynamic method to figure out your issue such as

[
  {
    "operation": "shift",
    "spec": {
      "myarray": {
        "*": {
          "v_518": {
            "@(1,&)": "&3.[&2].&",
            "@(1,lfdn)": "&3.[&2].lfdn"
          }
        }
      },
      "*": "&"
    }
  }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM