Support Questions

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

Unable to return a scalar value for the expression from NIFI EvaluateJsonPath processor

avatar
New Contributor

I'm using nifi to process json using Apache NIFI.

This is the input json:

{"key": "C919", "cells": [["year","2017",1497999002676770]]}

I'm using the following jsonpath expression:

$.cells[?(@[0]=='year')][1]

And in evaluatejsonpath processor, I got the following error:

EvaluateJsonPath[id=015c1006-dd8a-1d92-5eb2-9ed9822065f8] Unable to return a scalar value for the expression $['cells'][?][1] for FlowFile 69572. Evaluated value was [1999]. Transferring to failure.

But the return value is a scalar value here, It is 1999. And if I change the json expression to $.cells[?(@[0]=='year')][1][0]. It not allowed and throws error.

EvaluateJsonPath[id=015c1006-dd8a-1d92-5eb2-9ed9822065f8] EvaluateJsonPath[id=015c1006-dd8a-1d92-5eb2-9ed9822065f8] failed to process session due to com.jayway.jsonpath.InvalidPathException: Filter: [0] can only be applied to arrays. Current context is: 1999: com.jayway.jsonpath.InvalidPathException: Filter: [0] can only be applied to arrays. Current context is: 1999

Is it a bug in NIFI?

1 ACCEPTED SOLUTION

avatar
Master Guru

The answer in this StackOverflow post refers to documentation saying an array will be returned; this appears to be happening after the JSONPath is evaluated (which is why appending a [0] does not work). The post also implies the answer: in NiFi, you can choose "json" as the Return Type and "flowfile-attribute" as the Destination in EvaluateJsonPath (let's say your dynamic property has key "my.attr" and a value of your JSONPath expression above), then follow that processor with an UpdateAttribute processor, setting "my.attr" to the following:

${my.attr:jsonPath('$[0]')}

This will overwrite the original "my.attr" value by hoisting the value out of the array. If you need that value back in the content, you can follow this with a ReplaceText processor to replace the Entire Text with the value of "my.attr".

View solution in original post

1 REPLY 1

avatar
Master Guru

The answer in this StackOverflow post refers to documentation saying an array will be returned; this appears to be happening after the JSONPath is evaluated (which is why appending a [0] does not work). The post also implies the answer: in NiFi, you can choose "json" as the Return Type and "flowfile-attribute" as the Destination in EvaluateJsonPath (let's say your dynamic property has key "my.attr" and a value of your JSONPath expression above), then follow that processor with an UpdateAttribute processor, setting "my.attr" to the following:

${my.attr:jsonPath('$[0]')}

This will overwrite the original "my.attr" value by hoisting the value out of the array. If you need that value back in the content, you can follow this with a ReplaceText processor to replace the Entire Text with the value of "my.attr".