Search code examples
jsonapache-nifijolt

Jolt - Get values using a list of keys, alternatives to @(2,@)


I need to create a JSON array in order to split it into several jobs with Nifi. The array needs to be created based on an existing array inside the JSON.

Can't figure out how to dynamically create a reference to another object in the JSON. I want the reference "@(2,@)" to work, but this is not supported.

INPUT

{
  "name": "Loki",
  "id": "1234",
  "loc": "Utgard",
  "age": "unknown",
  "listitems": [
    "name",
    "id"
  ]
}

SPEC (that doesn't work):

[
  {
    "operation": "shift",
    "spec": {
      // Loop all listitems
      "listitems": {
        "*": {
          // Get the value of the current item and push to processlist.type array
          "@": "processlist[#2].type", 
          // Here is the problem, I need to get the "top level" value for the current value/key
          "@(2,@)": "processlist[#2].value"
        }
      }
    }
  }
]

Expected output:

{
  "processlist" : [ 
  {
    "type" : "name",
    "value" : "Loki"
  }, {
    "type" : "id",
    "value" : "1234"
  } 
  ]
}

SPEC (that will run but is not correct)

[
  {
    "operation": "shift",
    "spec": {
      // Loop all listitems
      "listitems": {
        "*": {
          // Get the value of the current item and push to processlist.type array
          "@": "processlist[#2].type", 
          // Here is the problem, I need to get the top level value for the current value/key
          // Forcing this to "name" will at least execute the code
          "@(2,name)": "processlist[#2].value"
        }
      }
    }
  }
]

Any ideas?


Solution

  • You can proceed one more step by adding "*" key to nest the current spec more while roaming by @(3,&) dynamically as this ampersand represents the incurred key values name and id such as

    [
      {
        "operation": "shift",
        "spec": {
          "listitems": {
            "*": {
              "*": {
                "@1": "processlist[#3].type",
                "@(3,&)": "processlist[#3].value"
              }
            }
          }
        }
      }
    ]