Problem looping through json result

Hi,

I´m trying to loop trough a JSON-Array with the value of numberIntakeTimes (which is also from the same JSON-Service). The value of numberIntakeTimes represents the number of items in the JSON-Array. But unfortunately OpenHAB is throwing an Exception.

This is the json result:

{"intaketime":[{"intakeTime":"1482912840","medicineID":28,"intakeTimeID":133},{"intakeTime":"1482945180","medicineID":27,"intakeTimeID":134}],"numberIntakeTimes":2}

And this is my rule file:

   
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*
import java.lang.Integer.*
import java.lang.Long.*
import java.lang.Math

// Test JSONPath
rule "Test JSONPath"
when 
	Time cron "0 0/1 * * * ?"   // every minute
then
  var httpG = sendHttpGetRequest("http://localhost:8080/smartmedicine/rest/medicineinformation/getIntakeTimeInformation")
  var long intakeTimeUnixTimeStamp = 0
  var int numberIntakeTime = Integer::parseInt(transform("JSONPATH","$.numberIntakeTimes", httpG))
  
  var long currentUnixTimeStamp = now.getMillis() / 1000L
  
   for (i : 0 .. numberIntakeTime){
     intakeTimeUnixTimeStamp = Long::parseLong(transform("JSONPATH","$.intaketime["+i+"].intakeTime", httpG))
	  if(currentUnixTimeStamp<intakeTimeUnixTimeStamp){
	    println("smaller")
	    println("current "+currentUnixTimeStamp+" unix "+intakeTimeUnixTimeStamp)
	  } else {
	  	println("bigger")
	    println("current "+currentUnixTimeStamp+" unix "+intakeTimeUnixTimeStamp)
	  }
  }
     
end 

And this is the Exception

2016-12-28 18:13:00.021 [ERROR] [o.o.c.t.actions.Transformation] - Error executing the transformation 'JSONPATH': An error occured while transforming JSON expression.
2016-12-28 18:13:00.023 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule Test JSONPath
java.lang.NumberFormatException: For input string: "{"intaketime":[{"intakeTime":"1483100220","medicineID":27,"intakeTimeID":130},{"intakeTime":"1483194360","medicineID":27,"intakeTimeID":131},{"intakeTime":"1482912480","medicineID":32,"intakeTimeID":132},{"intakeTime":"1482912840","medicineID":28,"intakeTimeID":133}],"numberIntakeTimes":4}"
        at java.lang.NumberFormatException.forInputString(Unknown Source) ~[na:1.8.0_112]
        at java.lang.Long.parseLong(Unknown Source) ~[na:1.8.0_112]
        at java.lang.Long.parseLong(Unknown Source) ~[na:1.8.0_112]
        at sun.reflect.GeneratedMethodAccessor71.invoke(Unknown Source) ~[na:na]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_112]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_112]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeOperation(XbaseInterpreter.java:729) ~[na:na]

thanks in advance :wink:

I would need a similar loop.
Have you been able to sovle your issue?

The original problem above is the OP was trying to parse a string as a number, which will always fail.

So don’t do that.