Archives of Support Questions (Read Only)

This is an archived board for historical reference. Information and links may no longer be available or relevant
Announcements
This board is archived and read-only for historical reference. To ask a new question, please post a new topic on the appropriate active board.

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

avatar
New Member

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