Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]

Support Questions

Find answers, ask questions, and share your expertise
Announcements
Share your experience with Cloudera on G2 and get a $25 Amazon Gift card.
Hi, I'm CLEO! Something exciting is coming to the Community. Stay Tuned!

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