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.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---