Iterate through json with javascript (forEach)

  • Platform information:
    • Hardware: Raspberry Pi 4 with 4GB
    • OS: Openhabian
    • Java Runtime Environment:OpenJDK Runtime Environment Zulu11.
    • openHAB version: 3.1.0.M5

I have a json-object which I like to evaluate with javascript. I wrote a script (or copied it from different places of the internet xD) to get the specific values from the object. The script should be used in transformation in the later process.

My code looks like this:

var json = [{"localNumber":"012345","remoteNumber":"012345","date":"2021-06-26T17:55:00+02","type":2,"duration":0}]
var mCalls = '';
var i = 1;
json.forEach((item) => {
  if (item.type == 2) {
  console.log('Anrufe: ' + item.remoteNumber);

  mCalls +=  'Anruf '+ i + 'am '+ item.date+ ' von Nummer ' + item.remoteNumber.toString() + ' ';
	console.log(mCalls);
	i = i+1;
  }
})

When i run it on https://onecompiler.com/ works.
If i run it in the script editor from openhab i get the following error message

2021-06-26 18:34:40.927 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID '2e31e320ac' failed: <eval>:4:20 Expected , but found =>
json.forEach((item) => {
                    ^ in <eval> at line number 4 at column number 20

I am not a developer an by far not a developer by for javascript.

Any hints and any help i appreciated.

Markus

I am not a developer either but I think your code is java and not javascript?

Something like this should work:

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);


var test = JSON.parse('{"localNumber":"012345","remoteNumber":"012345","date":"2021-06-26T17:55:00+02","type":2,"duration":0}') 
logger.info("test: " + test.remoteNumber)
logger.info("date: " + test.date)

Gives this result:

2021-06-27 09:42:03.122 [INFO ] [org.openhab.rule.scratchpad         ] - test: 012345
2021-06-27 09:42:03.123 [INFO ] [org.openhab.rule.scratchpad         ] - date: 2021-06-26T17:55:00+02

If you copy and paste the above example it should work.

I’m not sure, but I think OH uses an older JavaScript version (ES5?) which doesn’t support the => way of defining functions. Try

json.forEach(function(item) {

Here is some example code that I use successfully:

var logger = Java.type("org.slf4j.LoggerFactory").getLogger("scripts.unifi.all");

var deviceInfo = JSON.parse(newState)

var updateApState = function(itemName, state){
  logger.info("Setting " + itemName + " state to " + state);
  events.postUpdate(itemName, state);
};

deviceInfo.forEach(function(device){
  if (device._id == "5dad867146e0fb0013ac4545") {
    // Ground Floor AP
    updateApState('access_point_eg_switch', device.disabled ? "OFF" : "ON");
  } else if (device._id == "60b741e9a825190018328417") {
    // First Floor AP
    updateApState('access_point_og_switch', device.disabled ? "OFF" : "ON");
  } else if (device._id == "5dad84df46e0fb0013ac4528") {
    // Top Floor AP
    updateApState('access_point_dg_switch', device.disabled ? "OFF" : "ON");
  }
});
1 Like

Thanks very much guys. You are awesome .
It worked like a charme.