MikeThomsen commented on a change in pull request #3646: NIFI-6546 - Add 
JsonPath set value support
URL: https://github.com/apache/nifi/pull/3646#discussion_r317400876
 
 

 ##########
 File path: 
nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/JsonPathSetEvaluator.java
 ##########
 @@ -0,0 +1,57 @@
+/*
+ * 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 com.jayway.jsonpath.DocumentContext;
+import com.jayway.jsonpath.JsonPath;
+import org.apache.nifi.attribute.expression.language.EvaluationContext;
+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.StringQueryResult;
+
+/**
+ * JsonPathSetEvaluator allows setting values at the specified existing path
+ */
+public class JsonPathSetEvaluator extends JsonPathBaseEvaluator {
+
+    protected Evaluator<?> valueEvaluator;
+
+    public JsonPathSetEvaluator(final Evaluator<String> subject, final 
Evaluator<String> jsonPathExp, final Evaluator<?> valueEvaluator) {
+        super(subject, jsonPathExp);
+        this.valueEvaluator = valueEvaluator;
+    }
+
+    @Override
+    public QueryResult<String> evaluate(EvaluationContext context) {
+        DocumentContext documentContext = getDocumentContext(context);
+        final JsonPath compiledJsonPath = getJsonPath(context);
+
+        final Object value = valueEvaluator.evaluate(context).getValue();
+
+        String result;
+        try {
+            result = documentContext.set(compiledJsonPath, value).jsonString();
+        } catch (Exception e) {
+            // assume the path did not match anything in the document
 
 Review comment:
   Having an error logger here might be a good idea because I think the jayway 
jsonpath API can throw errors on invalid JsonPath and things like that.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to