I happened to be playing with this Friday... You can try single quotes in
the jsonPath as well.

Brandon
On Fri, Apr 27, 2018 at 5:05 PM Otto Fowler <[email protected]> wrote:

> $.fields[?(@.name==Type)].maskType
>
>
> no \” \”
>
> When JsonPath evaluates the path against the content, the content has
> already been
> parsed into a Map.
> So, when Jackson parses the json   “name” : “Type”  it ends up in a map
> that has those strings,
> but NOT the literal quotes in the string.
>
> When you pass in \”Type\”  it ends up doing
> if ( “Type” == “\”Type\”” )  ** not with the \ but the embedded quotes
>
> and  that is false.
>
>
> On April 27, 2018 at 16:41:51, Anil Rai ([email protected]) wrote:
>
> Thanks Otto.
> I did try changing the json path to :
> $.fields[?(@.name==\"Type\")].maskType
> I am still getting [] output.
> I am I missing something?
>
>
> On Fri, Apr 27, 2018 at 4:32 PM, Otto Fowler <[email protected]>
> wrote:
>
> > For the record, I am not saying that the json path library version nifi
> > uses isn’t broken because this doesn’t work.
> >
> >
> >
> > On April 27, 2018 at 16:30:57, Otto Fowler ([email protected])
> > wrote:
> >
> > 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
> > > > >
> > > >
> > >
> >
>

Reply via email to