Support Questions

Find answers, ask questions, and share your expertise

NiFi - Need help with JOLT-Syntax JoltTransformJSON

avatar
Master Collaborator

Hi, I need some help with JOLT-syntax.

JSON-Input

{
  "nummer": "1073",
  "table": [
    {
      "datum": "20180529",
      "zn": 1
    },
    {
      "datum": "20180530",
      "zn": 2
    },
    {
      "datum": "20190522",
      "zn": 3
    }
  ]
}


JOLT-Specification

[{
    "operation": "shift",
    "spec": {
      "nummer": "Nummer",
      "table": {
        "*": {
          "zn": "Positionen.[].ZeileNr",
          "datum": "Positionen.[].Datum"
        }
      }
    }
}
]


JSON-Output

{
    "Nummer": "1073",
    "Positionen": [{
        "ZeileNr": 1
    }, {
        "Datum": "20180529"
    }, {
        "ZeileNr": 2
    }, {
        "Datum": "20180530"
    }, {
        "ZeileNr": 3
    }, {
        "Datum": "20190522"
    }]
}

But the JSON-Output what I need is:


{
    "Nummer": "1073",
    "Positionen": [{
        "ZeileNr": 1,
        "Datum": "20180529"
    }, {
        "ZeileNr": 2,
        "Datum": "20180530"
    }, {
        "ZeileNr": 3,
        "Datum": "20190522"
    }]
}

Please, how can I manage this, it is driving me crazy...

Thanks for any help and information about this syntax!

1 ACCEPTED SOLUTION

avatar
Master Guru

You were so close! By using the [] syntax it just adds to the outgoing array, but you wanted to associate them with the same index, namely the one matched by the * "above" the fields. Put #2 inside your braces (#2 is a reference to the array index you're iterating over, "two levels up" from where you are in the spec):

[{
    "operation": "shift",
    "spec": {
      "nummer": "Nummer",
      "table": {
        "*": {
          "zn": "Positionen.[#2].ZeileNr",
          "datum": "Positionen.[#2].Datum"
        }
      }
    }
}]

View solution in original post

1 REPLY 1

avatar
Master Guru

You were so close! By using the [] syntax it just adds to the outgoing array, but you wanted to associate them with the same index, namely the one matched by the * "above" the fields. Put #2 inside your braces (#2 is a reference to the array index you're iterating over, "two levels up" from where you are in the spec):

[{
    "operation": "shift",
    "spec": {
      "nummer": "Nummer",
      "table": {
        "*": {
          "zn": "Positionen.[#2].ZeileNr",
          "datum": "Positionen.[#2].Datum"
        }
      }
    }
}]