Created 06-21-2017 12:35 PM
I use JSON Paths as attributes to nifi flowfiles (of JSON mime type). I want to evaluate these JSON Path over the content of the flowfiles but I can't use Evaluate JSON path processor as it does not support expression language. How can I do this as I can't hard code the JSON Paths in the processor, they are varying file to file.
Created on 06-21-2017 05:27 PM - edited 08-17-2019 08:36 PM
You can accomplish this with ExecuteScript, the following example uses Groovy as the language and Jayway's JsonPath as the library for JSONPath parsing. First I had to download 3 JARs (json-path and its required transitive dependencies) into a directory:
Then I set the Module Directory property to the path of this directory:
Note that I have also added a dynamic property, whose name will become an attribute and whose value supports Expression Language and (after evaluation) should contain a JSONPath expression used to retrieve the value from the content of the JSON flow file.
The Script Body is the following Groovy script:
import com.jayway.jsonpath.* def flowFile = session.get() if(!flowFile) return def inputStream = session.read(flowFile) def json = JsonPath.parse(inputStream) inputStream.close() context.properties.findAll {p,s -> p.dynamic}.each {pd, name -> def prop = context.getProperty(pd) try { flowFile = session.putAttribute(flowFile, pd.name, json.read(prop.evaluateAttributeExpressions(flowFile).value)) } catch (e) { log.error("Error evaluating JSONPath expression in property $name: ${prop?.value} , ignoring...", e) } } session.transfer(flowFile, REL_SUCCESS)
I tested this with a GenerateFlowFile:
After the ExecuteScript transfers the flow file, it has the desired attribute name/value:
This should work with any number of attributes/JSONPaths per flow file. In addition, I have written NIFI-4100 to cover the improvement to the EvaluateJsonPath processor to support Expression Language.
Created 06-22-2017 08:43 AM
There is such a scene,an attribute IP address in the flowfile,I have to call a Python function,to resolve the region infomation through IP address,then add the region infomation to new flowfile,how should I do? Thanks a lot for replying.@Matt Burgess
Created on 06-22-2017 08:50 AM - edited 08-17-2019 08:36 PM
I don't know why I can't ask question
Created 02-04-2020 05:10 AM
Hi @mburgess I tried the solution but it is not working for other JSON paths like $.key
I tried multiple JSON paths but seems like it only works for $.cells.year. Please let me know your thoughts. I followed the exact steps you provided.