Support Questions

Find answers, ask questions, and share your expertise

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

avatar

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)?

1 ACCEPTED SOLUTION

avatar
Master Guru

@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.

View solution in original post

4 REPLIES 4

avatar
Master Guru

@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.

avatar

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.

avatar
Master Guru

@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.

avatar
New Contributor

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