# Iterate through json with javascript (forEach)

**URL:** <https://community.openhab.org/t/iterate-through-json-with-javascript-foreach/123966>\
**Category:** Scripts & Rules\
**Created:** [June 26, 2021, 4:37pm UTC](https://community.openhab.org/t/iterate-through-json-with-javascript-foreach/123966 "2021-06-26T16:37:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mercator1990](https://community.openhab.org/user_avatar/community.openhab.org/mercator1990/32/59616_2.png) [@Mercator1990](https://community.openhab.org/u/Mercator1990)\
**Post date:** [June 26, 2021, 4:37pm UTC](https://community.openhab.org/t/iterate-through-json-with-javascript-foreach/123966/1 "2021-06-26T16:37:31Z")

</div>

- 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:

```auto
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/](https://onecompiler.com/) works.  
If i run it in the script editor from openhab i get the following error message

```auto
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

---

<div class="post-metadata">

**Author:** ![ubeaut](https://community.openhab.org/user_avatar/community.openhab.org/ubeaut/32/119014_2.png) [@ubeaut](https://community.openhab.org/u/ubeaut)\
**Post date:** [June 26, 2021, 11:31pm UTC](https://community.openhab.org/t/iterate-through-json-with-javascript-foreach/123966/2 "2021-06-26T23:31:06Z")

</div>

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

Something like this should work:

```javascript
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![pacive](https://community.openhab.org/user_avatar/community.openhab.org/pacive/32/36234_2.png) [@pacive](https://community.openhab.org/u/pacive)\
**Post date:** [June 27, 2021, 5:28am UTC](https://community.openhab.org/t/iterate-through-json-with-javascript-foreach/123966/3 "2021-06-27T05:28:51Z")

</div>

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

```auto
json.forEach(function(item) {

```

---

<div class="post-metadata">

**Author:** ![DrRSatzteil](https://community.openhab.org/user_avatar/community.openhab.org/drrsatzteil/32/63872_2.png) [@DrRSatzteil](https://community.openhab.org/u/DrRSatzteil)\
**Post date:** [June 27, 2021, 6:46am UTC](https://community.openhab.org/t/iterate-through-json-with-javascript-foreach/123966/4 "2021-06-27T06:46:25Z")

</div>

Here is some example code that I use successfully:

```auto
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");
  }
});

```

---

<div class="post-metadata">

**Author:** ![Mercator1990](https://community.openhab.org/user_avatar/community.openhab.org/mercator1990/32/59616_2.png) [@Mercator1990](https://community.openhab.org/u/Mercator1990)\
**Post date:** [June 27, 2021, 6:27pm UTC](https://community.openhab.org/t/iterate-through-json-with-javascript-foreach/123966/5 "2021-06-27T18:27:39Z")

</div>

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