The reason why your path is failing is because of the embedded quotes.
If you remove the quotes it will work.
I wrote this test:
@Test
public void testIssue() throws Exception{
String jsonPathAttrKey = "JsonPath1";
final TestRunner testRunner = TestRunners.newTestRunner(new
EvaluateJsonPath());
testRunner.setProperty(EvaluateJsonPath.DESTINATION,
EvaluateJsonPath.DESTINATION_CONTENT);
testRunner.setProperty("JsonPath1", "$.fields[?(@.name==Type)].maskType");
testRunner.enqueue(JSON_FAIL_SNIPPET);
testRunner.run();
Relationship expectedRel = EvaluateJsonPath.REL_MATCH;
testRunner.assertAllFlowFilesTransferred(expectedRel, 1);
final MockFlowFile out =
testRunner.getFlowFilesForRelationship(expectedRel).get(0);
out.assertContentEquals("[\"b\"]");
}
Here, JSON_FAIL_SNIPPET is your content as a file.
I understand that your original path works on the test web sites. It
*also* works if you call JsonPath differently:
/**{ "fields":[
{
"maskType": "a",
"name": "Id"
},
{
"maskType": "b",
"name": "Type"
}
]
}*/
@Multiline
static String NIFI_PROB;
@Test
public void testNifi() {
App app = new App();
List value = app.justJsonPath(NIFI_PROB.getBytes(),
"$.fields[?(@.name==\"Type\")].maskType");
if( value != null ) {
value.forEach(new Consumer() {
@Override
public void accept(Object o) {
System.out.println(o.toString());
}
});
}
}
private final ObjectMapper objectMapper = new ObjectMapper();
public App() {
Configuration.setDefaults(new Configuration.Defaults() {
private final JsonProvider jsonProvider = new JacksonJsonProvider();
private final MappingProvider mappingProvider = new
JacksonMappingProvider();
@Override
public JsonProvider jsonProvider() {
return jsonProvider;
}
@Override
public MappingProvider mappingProvider() {
return mappingProvider;
}
@Override
public Set<Option> options() {
return EnumSet.noneOf(Option.class);
}
});
CacheProvider.setCache(new LRUCache(100));
}
public List justJsonPath(byte[] rawMessage, String jsonPath) {
return JsonPath.parse(new String(rawMessage))
.read(jsonPath);
}
b
Process finished with exit code 0
But the way the Nifi uses JsonPath you need to call it as such.
Hope this helps.
ottO
On April 27, 2018 at 16:09:32, Mike Thomsen ([email protected]) wrote:
The jayway JSONPath library on GitHub is the library I was referring to.
I'll check on Jira to see if there's a ticket. Seems like something we
could do for 1.7
Thanks,
Mike
On Fri, Apr 27, 2018 at 3:46 PM Anil Rai <[email protected]> wrote:
> Hi Mike,
> Thanks for your quick reply. I am using nifi 1.5.0 release. I am not sure
> what you mean by "trying against the latest version of java lib?"
> If you could further elaborate please?
> Also is there any other way to achieve this outcome (either by changing
the
> path or by using a completely different processor other than
> evaluatejsonpath?)
>
> Thanks
> Anil
>
>
> On Fri, Apr 27, 2018 at 3:37 PM, Mike Thomsen <[email protected]>
> wrote:
>
> > The JsonPath processor uses an older version of the Java library. I
think
> > it's v2.0. Have you tried that against the latest version of the Java
> lib?
> > I know there's at least one JsonPath feature the version we use doesn't
> > support.
> >
> > On Fri, Apr 27, 2018 at 3:14 PM Anil Rai <[email protected]> wrote:
> >
> > > Experts,
> > >
> > > Input JSON
> > > -------
> > > { "fields":[
> > > {
> > > "maskType": "a",
> > > "name": "Id"
> > > },
> > > {
> > > "maskType": "b",
> > > "name": "Type"
> > > }
> > > ]
> > > }
> > > -----------
> > >
> > > JsonPath : $.fields[?(@.name=="Type")].maskType
> > >
> > > --------------
> > >
> > > Expected Output
> > > -------
> > > [
> > > "b"
> > > ]
> > > --------
> > >
> > > But EvaluateJsonPath processor is giving me []
> > >
> > > Any idea?
> > >
> > > Thanks
> > > Anil
> > >
> >
>