Support Questions

Find answers, ask questions, and share your expertise

Merge Json and adjust resulting one with JoltTransform

avatar
Explorer

I would like to merge two different json in nifi. first json comes from an attribute to json processor and contains just an id, something like:

 {
    "id": "1234"
 }

second one is a json of this structure:

[
{"id1": "1234","id2": "1234","id3": "1234"},
{"id1": "1234","id2": "1234","id3": "1234"},
{"id1": "1234","id2": "1234","id3": "1234"},
{"id1": "1234","id2": "1234","id3": "1234"}
]

desired result should be something like:

{
"id": "1234",
"data": [
{"id1": "1234","id2": "1234","id3": "1234"},
{"id1": "1234","id2": "1234","id3": "1234"},
{"id1": "1234","id2": "1234","id3": "1234"},
{"id1": "1234","id2": "1234","id3": "1234"}
]
}

how can I make it? I was trying merge content to concatenate those two json flowfiles having this result:

[
  {
    "id": "1233"
  },
  [
    {
      "id1": "1234",
      "id2": "1234",
      "id3": "1234"
    },
    {
      "id1": "1234",
      "id2": "1234",
      "id3": "1234"
    },
    {
      "id1": "1234",
      "id2": "1234",
      "id3": "1234"
    },
    {
      "id1": "1234",
      "id2": "1234",
      "id3": "1234"
    }
  ]
]

now, I think I need a Jolt transform (or maybe something else ?!?) but I don't know which are the parameters of the transformation...

1 REPLY 1

avatar
Explorer

I found out this solution but when I apply it on nifi it returns an error saying " is invalid because specification not valid for the selected transformation

but it works on jolttransformation demo (https://jolt-demo.appspot.com/#inception😞

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "id": "id"
      },
      "#1": {
        "&": "result"
      }
    }
  }
]

I don't understand why