[SOLVED] Save JSON value to number variable

I am trying to save a value from a JSON message to a number variable so I can then preform some maths on it.

The message is coming form a String Item state.

The message is
{"state":"ON","brightness":255}

Following examples I am able to save these values to string variables but not number ones.

What is the correct syntax to save a JSON object to a number variable?

Thanks.

val String sJson = '{"state":"ON","brightness":255}'
val Number nBright = transform("JSONPATH","$.brightness",sJson)

I’ve just tried that but I still can’t preform any math on the value

rule "Convert JSON"
  when
    Item StudyLight_String changed
 then
    val String sJson = '{"state":"ON","brightness":255}'
    val Number nBright = transform("JSONPATH","$.brightness",sJson)
    val Number converted = (nBright * 100)
    logInfo("User.Testing","Converted = " + converted)
 end

When the rule runs I get this error in the log.

2019-03-19 18:07:06.987 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Convert JSON to Item Type Number': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.lib.NumberExtensions.operator_multiply(java.lang.Number,java.lang.Number) on instance: null

Ah, you misunderstood the lines :slight_smile:

rule "Convert JSON"
  when
    Item StudyLight_String changed
 then
    val String sJson = StudyLight_String.state
    val Number nBright = transform("JSONPATH","$.brightness",sJson)
    logInfo("User.Testing","nBright = {}", nBright)
    if(!(nBright instanceof Number)) return;
    val Number converted = nBright * 100
    logInfo("User.Testing","Converted = {}", converted)
 end

Still getting an error I’m afraid.

2019-03-19 18:26:00.089 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Convert JSON': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.core.transform.actions.Transformation.transform(java.lang.String,java.lang.String,java.lang.String) on instance: null

Thanks your for help.

EDIT

Changing the rule to the following stopped the error but it stops at the If.

rule "Convert JSON"
  when
    Item StudyLight_String changed
 then
    val String sJson = StudyLight_String.state.toString
    val Number nBright = transform("JSONPATH","$.brightness",sJson)
    logInfo("User.Testing","nBright = {}", nBright)
    if(!(nBright instanceof Number)) return;
    val Number converted = nBright * 100
    logInfo("User.Testing","Converted = {}", converted)
 end

So what’s the status of StudyLight_String?
Maybe add another logInfo:

rule "Convert JSON"
  when
    Item StudyLight_String changed
 then
    val String sJson = StudyLight_String.state.toString
    logInfo("User.Testing","sJson String = {}",sJson)

You will find the logging in openhab.log

2 Likes

In VS Code, this statement generates the error Type mismatch: Cannot convert from String to Number

I usually deal with it this way in my rules.

val Number nBright = Integer.parseInt(transform("JSONPATH","$.brightness",sJson))

If there’s a chance the return from transform will be non-numeric, you would want to catch NumberFormatException.

try {
    val Number nBright = Integer.parseInt(transform("JSONPATH","$.brightness",sJson))
} catch (NumberFormatException e) {
    // Do something if the value in non-numeric
}

The above is working.

Thank you very much.