[SOLVED] JSONPATH expression with String variable in the filter part

The timing of your post is uncanny.

This other thread had me really confused and I’ve played with JSONPATH now for hours and have finally seen the issue that was being reported, and you are experiencing exactly the same issue. There are many other ways to do what you are looking to do, but with a few changes I think it will work as you have it.

  1. Are vName and tVal global variables? If not, you’re missing the declarations.
  2. You’ll need the double quotes as @namraccr suggested.
  3. The values you are getting returned by JSONPATH have double quotes in them, so that needs to be taken into account when you are using the values in the filter expression. I’m still testing some things, but I believe a PR for the documentation needs to be made for this behavior.

Try this out and see if it works for you:

val String rData = MQTT_SENSOR.state.toString	//Recieved Data
val String rSensors = transform("JSONPATH", "$.DS18x20..Address", rData) // Recieved Sensors Addresses
val String[] rSensorsArr = rSensors.substring(1,rSensors.indexOf(']')).split(",")
for (String item: rSensorsArr) {
    val String i = item.replace('"','')
    if (jsonLiveSensors.contains(i) ) { //Check if ID is active
        logInfo("MQTT_SENSOR","Address:" + i + " OKE!")
        val String vName = transform("JSONPATH", "$.DS18x20.[?(@.Address=="' + i + '")].Name", jsonLiveSensors)
        //vName = vName.substring(1,vName.indexOf(']'))
        logInfo("MQTT_SENSOR","Variable Name:" + vName)
        val String tVal = transform("JSONPATH", "$.DS18x20..[?(@.Address=='"+i+"')].Temperature", rData)
        //vVal = new Double(tVal.substring(1,tVal.indexOf(']')))
        logInfo("MQTT_SENSOR","Variable Value:" + vVal)
        if(gTemperature.members.filter[k|k.name==vName].size > 0){
            val vItem = gTemperature.members.filter[k|k.name==vName].head
            vItem.sendCommand(vVal);
            logInfo("MQTT_SENSOR","SendCommand Update:"+vName)
        }else{
            logInfo("MQTT_SENSOR","SendCommand Fail:"+vName)
        }
    }else{
        logInfo("MQTT_SENSOR","Address:" + i + " NOT found")
    }
}