Search code examples
apache-nifijolt

JOLT - Transform into output array


I'm kinda new to JOLT and been struggling with getting the right transformation. Any help is appreciated. This is my input data:

{
  "data": {
    "names": [
      "columnA",
      "columnB",
      "columnC",
      "columnD",
      "columnE",
      "columnF",
      "columnG",
      "columnH",
      "columnI",
      "columnJ",
      "columnK"
    ],
    "ndarray": [
      [
        0,
        1,
        2,
        "aString",
        "bString",
        3,
        4,
        "Text in English (A)",
        "Text in German (A)",
        "Error id A",
        "Error text A"
      ],
      [
        10,
        11,
        12,
        "cString",
        "dString",
        13,
        14,
        "Text in English (B)",
        "Text in German (B)",
        "Error id B",
        "Error text B"
      ]
    ]
  }
}

My goal is to create an array that holds all the entries. For this example, there will be 2 entries (same length as data.ndarray). Array data.ndnames (keys) and data.ndarray[i] (values) are mapped 1:1, i.e. they should have the same length.

This is the expected output:

{
  "result": [
    {
      "columnA": 0,
      "columnG": 4,
      "columnC": 2,
      "columnF": 3,
      "explanation": [
        {
          "text": "Text in English (A)",
          "lang": "EN" // constant value
        },
        {
          "text": "Text in German (A)",
          "lang": "DE" // constant value
        }
      ],
      "columnD": "aString",
      "columnI": "Error id A",
      "columnJ": "Error text A"
    },
    {
      "columnA": 10,
      "columnG": 14,
      "columnC": 12,
      "columnF": 13,
      "explanation": [
        {
          "text": "Text in English (B)",
          "lang": "EN" // constant value
        },
        {
          "text": "Text in German (B)",
          "lang": "DE" // constant value
        }
      ],
      "columnD": "cString",
      "columnI": "Error id B",
      "columnJ": "Error text B"
    }
  ]
}

Solution

  • [EDIT] Try this spec:

    [
      {
        "operation": "shift",
        "spec": {
          "data": {
            "ndarray": {
              "*": {
                "*": {
                  "@": "result[&2].@(4,names[&1])"
                }
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "result": {
            "*": {
              "columnH": "result[&1].explanation[0].text",
              "#EN": "result[&1].explanation[0].lang",
              "columnI": "result[&1].explanation[1].text",
              "#DE": "result[&1].explanation[1].lang",
              "columnB": null,
              "columnE": null,
              "columnJ": "result[&1].columnI",
              "columnK": "result[&1].columnJ",
              "*": "result[&1].&"
            }
          }
        }
      }
    ]