Skip to main content
anony
Silver ⭐️⭐️⭐️
April 22, 2023
Question

How to iterate on an array and extract value in nifi?

How can I iterate on this array in NIfi, I am extracting this array from an API response in nifi but unable to extract every index value one by one for completing my whole flow.

Example: Need 6100 in any variable so that I can use it for further process and in next iteration need 6101 in that variable and so on.

Edit: This array is extracting like this only, it is not assigned to any named array such as arr[] or anything.

 

[ "6100", "6101", "6102", "6103", "6104", "6105", "6106", "6107", "6108", "6423", "23382", "354548" ]

10 replies

cotopaul
Platinum ⭐️
April 23, 2023

hi @anony,
Is this the content of your FlowFile coming from the API as a response?
If yes, you could split your array into several FlowFiles and process them in whatever order you would like.

 

I assume that you do not need your square brackets and your double quotes for your processing so you could use a ReplaceText to remove all of them.

In your ReplaceText Processor, you need to define a java regex which will perform this remove of double quotes and square brackets.

If you do not succeed in writing that java regex, you can try a combination of 4 ReplaceText processors:
- the first one defined as Search Value = " and Replacement Value set to empty string. Make sure you use Replacement Strategy = Literal Replace.
- the second one defined as Search Value =   (empty space here) and Replacement Value set to empty string. Make sure you use Replacement Strategy = Literal Replace.
- the third one defined as Search Value = [ and Replacement Value set to empty string. Make sure you use Replacement Strategy = Literal Replace.

- the forth one defined as Search Value = ] and Replacement Value set to empty string. Make sure you use Replacement Strategy = Literal Replace.

Once you removed all those, proceed with using a SplitContent, defined as follows:
- Byte Sequence Format = Text

- Byte Sequence = ,

This will send forward in your stream a flowfile containing each value from your initial Array.
Basically you will have 12 flowfiles, each with one value from your initial array.

PS: as you did not provide any information regarding the scope of your iteration, I have somehow ignored it as I did not fully understand what you meant.

Translate
anony
anonyAuthor
Silver ⭐️⭐️⭐️
April 23, 2023

Hi, I had done it and as you mentioned I will get 12 flowfiles, I got the same but the problem is I need only 1 value at a time as we do in a loop.

 

So the question is how to use the flowfile value for running the loop OR "how to run loop on flowfiles" ?

 

I have 12 flowfiles now for further process I need the 1st index values that is the first flowfile value and process further and in second time I need 2nd index value so that I can process further and 3rd need the index value and so on for all flow files. 

 

Now question rises how can I iterate on flowfiles?

Translate