jsonapache-nifi

How to route json into multiple attributes in Nifi?


I'm working in NiFi and I need to get data from excel file and convert it to json - I did that using ExecuteStreamCommand. The json I'm getting is fine for me. Here it is:

[ {
  "A" : "10560, 543, 267, 384, 0"
}, {
  "B" : "0, 50, 123, 203, 65, 56"
}, {
  "C" : "12, 1653, 665, 0, 32"
}, {
  "D" : "5446, 2321, 5543, 66533, 6543"
} ]

I need to route it to different attributes to get the following result:

FlowFile 1
{ "A" : "10560, 543, 267, 384, 0"}
FlowFile 2
{"B" : "0, 50, 123, 203, 65, 56"}

I need to be able to route file with "A" to one processor, file with "B" to another and etc. I don't know how to get such result in NiFi. RouteOnAttribute or EvaluateJsonPath or SplitJson don't seem to work. RouteOnContent doesn't work too. I tried to extract it to text but the best result I could get is attribute with text contained all json, not separate attributes.

Also numbers in excel aren't static, they can be changed, so numbers in my json aren't static too.

I am also thinking about ExtractText with RegEx, but unfortunately I know almost nothing about it. I tried (as I mentioned above) to use it, but could only get all the json as text.


Solution

  • I am not sure if I understand what you want. As I understand, you want to split one Flowfile containing an array of objects into mutliple Flowfiles containing each one object of the array.

    This could be done by using the SplitJson Processor with a JsonPath Expression of $.

    You have then:

    Flowfile 1
    { "A" : "10560, 543, 267, 384, 0"}
    FlowFile 2
    {"B" : "0, 50, 123, 203, 65, 56"}
    Flowfile 3
    {"C" : "12, 1653, 665, 0, 32"}
    Flowfile 4
    {"D" : "5446, 2321, 5543, 66533, 6543"}
    

    If you want to route them to different processors you can do that by using the RouteOnContent Processor, after the SplitJson.

    Here you can define, for example, a custom propertie A with the value .*A.* which will result in a route called A where any flowfile is routed that contains at least an A character. In your example Json this would be fine, but maybe there is the need for a better regular expression.