/**
 * Copyright © 2016-2022 The Thingsboard Authors
 *
 * Licensed 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.thingsboard.rule.engine.transform;

import com.fasterxml.jackson.databind.JsonNode;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider;
import lombok.extern.slf4j.Slf4j;
import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.rule.engine.api.RuleNode;
import org.thingsboard.rule.engine.api.TbContext;
import org.thingsboard.rule.engine.api.TbNode;
import org.thingsboard.rule.engine.api.TbNodeConfiguration;
import org.thingsboard.rule.engine.api.TbNodeException;
import org.thingsboard.rule.engine.api.util.TbNodeUtils;
import org.thingsboard.server.common.data.plugin.ComponentType;
import org.thingsboard.server.common.msg.TbMsg;

import java.util.concurrent.ExecutionException;

@Slf4j
@RuleNode(
        type = ComponentType.TRANSFORMATION,
        name = "json path",
        configClazz = TbJsonPathNodeConfiguration.class,
        nodeDescription = "Extracts json element or set of elements from a message by JSONPath expression.",
        nodeDetails = "JSONPath expression specifies a path to an element or a set of elements in a JSON structure. <br/>"
                + "<b>'$'</b> represents the root object or array. If JSONPath expression evaluation failed, incoming "
                + "age routes via <code>Failure</code> chain, otherwise <code>Success</code> chain is used.",
        uiResources = {"static/rulenode/rulenode-core-config.js"},
        icon = "functions",
        configDirective = "tbTransformationNodeJsonPathConfig"
)
public class TbJsonPathNode implements TbNode {

    TbJsonPathNodeConfiguration config;
    Configuration configurationJsonPath;
    JsonPath jsonPath;

    @Override
    public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
        this.config = TbNodeUtils.convert(configuration, TbJsonPathNodeConfiguration.class);
        this.configurationJsonPath = Configuration.builder()
                .jsonProvider(new JacksonJsonNodeJsonProvider())
                .build();
        this.jsonPath = JsonPath.compile(config.getJsonPath());
    }

    @Override
    public void onMsg(TbContext ctx, TbMsg msg) throws ExecutionException, InterruptedException, TbNodeException {
        try {
            JsonNode jsonPathData = jsonPath.read(msg.getData(), this.configurationJsonPath);
            ctx.tellSuccess(TbMsg.transformMsg(msg, msg.getType(), msg.getOriginator(), msg.getMetaData(), JacksonUtil.toString(jsonPathData)));
        } catch (PathNotFoundException e) {
            ctx.tellFailure(msg, e);
        }
    }

    @Override
    public void destroy() {

    }
}

@@ -1980,16 +1980,20 @@
      + %22
+mess
 age rout
@@ -2387,16 +2387,42 @@
 sonPath;
+%0A    String jsonPathValue;
 %0A%0A    @O
@@ -2618,16 +2618,112 @@
 class);%0A
+        this.jsonPathValue = config.getJsonPath();%0A        if (this.jsonPathValue != %22$%22) %7B%0A    
         
@@ -2775,16 +2775,20 @@
 ilder()%0A
+    
         
@@ -2860,16 +2860,20 @@
         
+    
 .build()
@@ -2874,32 +2874,36 @@
 uild();%0A        
+ 
+   
 this.jsonPath = 
@@ -2938,24 +2938,34 @@
 sonPath());%0A
+        %7D%0A
     %7D%0A%0A    @
@@ -3080,32 +3080,72 @@
 NodeException %7B%0A
+        if (jsonPathValue != %22$%22) %7B%0A    
         try %7B%0A  
@@ -3142,16 +3142,20 @@
   try %7B%0A
+    
         
@@ -3244,32 +3244,36 @@
 h);%0A            
+   
+ 
 ctx.tellSuccess(
@@ -3397,16 +3397,20 @@
         
+    
 %7D catch 
@@ -3441,32 +3441,36 @@
 ) %7B%0A            
+    
 ctx.tellFailure(
@@ -3478,16 +3478,81 @@
 sg, e);%0A
+            %7D%0A        %7D else %7B%0A            ctx.tellSuccess(msg);%0A
         

add check value "$"