Skip to main content
patrick_hochste
Silver ⭐️
May 29, 2019 9:14 PM UTC
Solved

NiFi: How to extract the size of a JSON array into an attribute?

In my flow file I have JSON content which contains an array. How can I extract the size of that array into an attribute (e.g. my.array.size = 5)?

Shu_ashu
Shu_ashuAnswer
Gold ⭐️⭐️⭐️
May 30, 2019 8:59 AM UTC

@Patrick Hochstenbach

You can use SplitJson processor to split the array($.*) into individual flowfile then split json processor adds fragment.count attribute to the flowfile which is array size.

Use UpdateAttribute processor to change the attribute name.

Use MergeContent processor to merge back the content using Defragment strategy.

4 replies

Shu_ashu
Shu_ashuAnswer
Gold ⭐️⭐️⭐️
May 30, 2019 8:59 AM UTC

@Patrick Hochstenbach

You can use SplitJson processor to split the array($.*) into individual flowfile then split json processor adds fragment.count attribute to the flowfile which is array size.

Use UpdateAttribute processor to change the attribute name.

Use MergeContent processor to merge back the content using Defragment strategy.

Translate
patrick_hochste
Silver ⭐️
Jun 3, 2019 2:07 PM UTC

Thanks, I was hoping for an easy trick because I needed many of these calculations. Maybe I need to write a custom processor for this.

Translate
Shu_ashu
Gold ⭐️⭐️⭐️
Jun 4, 2019 9:58 AM UTC

@Patrick Hochstenbach

Yes,You can consider custom processor/custom script for this, to do this in easier way and add the attribute to the flowfile with array size.

Translate
antonisr11
Silver ⭐️
Oct 13, 2022 7:28 PM UTC

You can use EvaluateJsonPath. You only have to add an extra attribute and name it as you want (e.g. count) and set as value "$.my.array.size.length()". Then select Destination and change it to flowfile-attribute. This processor will produce a flowfile which has your extra attribute

Translate