Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

I have to split a json into two using nifi processors

avatar
Explorer

I want to split this json

{"id": "652fbf430f1f3f30a3111f11,652fbf430f1f3f30a3333f11", "Qid": 123} into

{"id": "652fbf430f1f3f30a3111f11", "Qid": 123} and {"id": "652fbf430f1f3f30a3333f11","Qid": 123}

using Apache Nifi in the above example we have two id values there can be multiple values as well then I want to add these id values to the attribute of the two flow files that are created from the split JSONs.

1 REPLY 1

avatar

To do the split , you need to transform the json input into a format that allows you to do so, for that you can use JoltTransformationJson with the following Jolt Specification:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "ids": "=split('[,]',@(1,id))"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "ids": {
        "*": {
          "@": "[&1].id",
          "@(2,Qid)": "[&1].Qid"
        }
      }
    }
  }
]

The spec above will generate the following output:

[
	{
		"id": "652fbf430f1f3f30a3111f11",
		"Qid": 123
	},
	{
		"id": "652fbf430f1f3f30a3333f11",
		"Qid": 123
	}
]

Then you can use SplitJson processor where the JsonPath Expression is set to $

To get the id attribute you can use EvaluateJsonPath as follows:

SAMSAL_0-1698931918590.png

The flowfile_id is dynamic property where the value is the json path to the id of the json input. Make sure to set the Destination property to "flowfile-attribute".

If that helps please accept solution.

Thanks