Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]
Created 06-21-2017 02:57 PM
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?
Created 06-21-2017 04:33 PM
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".
Created 06-21-2017 04:33 PM
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".