[ 
https://issues.apache.org/jira/browse/NIFI-1660?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15296455#comment-15296455
 ] 

ASF GitHub Bot commented on NIFI-1660:
--------------------------------------

Github user markap14 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/303#discussion_r64234770
  
    --- Diff: 
nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/JsonPathEvaluator.java
 ---
    @@ -0,0 +1,98 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.nifi.attribute.expression.language.evaluation.functions;
    +
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Objects;
    +
    +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator;
    +import 
org.apache.nifi.attribute.expression.language.evaluation.QueryResult;
    +import 
org.apache.nifi.attribute.expression.language.evaluation.StringEvaluator;
    +import 
org.apache.nifi.attribute.expression.language.evaluation.StringQueryResult;
    +
    +import com.jayway.jsonpath.Configuration;
    +import com.jayway.jsonpath.DocumentContext;
    +import com.jayway.jsonpath.InvalidJsonException;
    +import com.jayway.jsonpath.JsonPath;
    +import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
    +import com.jayway.jsonpath.spi.json.JsonProvider;
    +
    +
    +public class JsonPathEvaluator extends StringEvaluator {
    +
    +    private static final StringQueryResult NULL_RESULT = new 
StringQueryResult("");
    +    private static final Configuration STRICT_PROVIDER_CONFIGURATION = 
Configuration.builder().jsonProvider(new JacksonJsonProvider()).build();
    +    private static final JsonProvider JSON_PROVIDER = 
STRICT_PROVIDER_CONFIGURATION.jsonProvider();
    +
    +    private final Evaluator<String> subject;
    +    private final Evaluator<String> jsonPathExp;
    +
    +    public JsonPathEvaluator(final Evaluator<String> subject, final 
Evaluator<String> jsonPathExp) {
    +        this.subject = subject;
    +        this.jsonPathExp = jsonPathExp;
    +    }
    +
    +    @Override
    +    public QueryResult<String> evaluate(final Map<String, String> 
attributes) {
    +        final String subjectValue = 
subject.evaluate(attributes).getValue();
    +        if (subjectValue == null || subjectValue.length() == 0) {
    +            return NULL_RESULT;
    +        }
    +        DocumentContext documentContext = null;
    +        try {
    +            documentContext = 
validateAndEstablishJsonContext(subjectValue);
    +        } catch (InvalidJsonException e) {
    +            return NULL_RESULT;
    +        }
    +
    +        final JsonPath compiledJsonPath = 
JsonPath.compile(jsonPathExp.evaluate(attributes).getValue());
    --- End diff --
    
    In the vast majority of use cases, I would expect that the JSON Path will 
be a literal string that does not reference any attributes. In this case, I 
would want to avoid compiling the JSON Path every time. Would recommend that if 
the jsonPathExp is an instance of the StringLiteralEvaluator, we pre-compile 
the JSON Path. We follow a similar pattern with the MatchesEvaluator for 
pre-compiling regular expressions, and it can make a pretty huge difference on 
performance.


> Enhance the expression language with jsonPath function
> ------------------------------------------------------
>
>                 Key: NIFI-1660
>                 URL: https://issues.apache.org/jira/browse/NIFI-1660
>             Project: Apache NiFi
>          Issue Type: Improvement
>          Components: Core Framework
>    Affects Versions: 0.5.1
>            Reporter: Christopher McDermott
>            Assignee: Joseph Witt
>            Priority: Minor
>             Fix For: 1.0.0, 0.7.0
>
>
> jsonPath would evaluate a JSON path provided, as an argument, against the 
> subject.
> Example
> {quote}
> $\{kafka.key:jsonPath('$.foo.bar')\}
> {quote}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to