Parse JSON array within rule's action DSL

  • Platform information:
    • Hardware: Raspberry Pi 4
    • OS: Raspberry Pi OS / x64 / 11 (bullseye)
    • Java Runtime Environment: 11.0.15 (Temurin-11.0.15+10) (running in Docker)
    • openHAB version: 3.4.4 (running in Docker)
    • ConBee 2 USB stick

Dear community,

After this question, I’m trying to implement the suggested solution (delete orphaned user session via REST API). From an external PowerShell script, it works nicely.

Since I prefer to do the cleanup within openHAB, I want to implement a rule based in openHAB’s DSL which runs every Sunday. This is my current code:

var headers = newHashMap("Authorization" -> "Bearer oh.<<SuperSecret>>");
var output = sendHttpGetRequest("http://127.0.0.1:8080/rest/auth/sessions", headers, 1000);
logInfo("rules", output);
var sessionId = transform("JSONPATH", "$.[3].sessionId", output);
logInfo("rules", 'Bla')
logInfo("rules", "sessionId: " + sessionId)
logInfo("rules", 'Blub')

There seems to be no syntactic error and this is the log output after running the rule:

12:54:35.760 [INFO ] [org.openhab.core.model.script.rules  ] - [{"sessionId":"bf557728","createdTime":"2023-06-13T17:11:56.000+0200","lastRefreshTime":"2023-06-27T17:29:26.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"ab260658","createdTime":"2023-06-21T18:37:11.000+0200","lastRefreshTime":"2023-07-08T23:02:22.000+0200","clientId":"https://192.168.180.33:11443","scope":"admin"},{"sessionId":"1929c769","createdTime":"2023-06-26T18:04:02.000+0200","lastRefreshTime":"2023-06-26T18:05:17.000+0200","clientId":"https://myRaspi:11443","scope":"admin"},{"sessionId":"1d37ea5b","createdTime":"2023-06-27T17:32:54.000+0200","lastRefreshTime":"2023-07-01T16:24:22.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"60c27f8f","createdTime":"2023-07-01T16:28:19.000+0200","lastRefreshTime":"2023-07-01T18:15:55.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"2e4922ed","createdTime":"2023-07-01T17:29:32.000+0200","lastRefreshTime":"2023-07-01T17:29:43.000+0200","clientId":"https://myRaspi:11443","scope":"admin"},{"sessionId":"4d503deb","createdTime":"2023-07-01T18:18:41.000+0200","lastRefreshTime":"2023-07-01T20:40:13.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"eb7721cc","createdTime":"2023-07-01T20:03:09.000+0200","lastRefreshTime":"2023-07-01T20:03:20.000+0200","clientId":"https://myRaspi:11443","scope":"admin"},{"sessionId":"8332f5e6","createdTime":"2023-07-01T20:42:44.000+0200","lastRefreshTime":"2023-07-02T16:33:55.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"8e71361b","createdTime":"2023-07-02T16:08:52.000+0200","lastRefreshTime":"2023-07-02T16:09:02.000+0200","clientId":"https://myRaspi:11443","scope":"admin"},{"sessionId":"c0fca1c5","createdTime":"2023-07-02T16:37:52.000+0200","lastRefreshTime":"2023-07-02T16:43:20.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"fe6b52ef","createdTime":"2023-07-02T16:46:50.000+0200","lastRefreshTime":"2023-07-02T17:22:23.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"29e4e9c5","createdTime":"2023-07-02T17:25:30.000+0200","lastRefreshTime":"2023-07-03T15:45:00.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"db524553","createdTime":"2023-07-03T16:22:05.000+0200","lastRefreshTime":"2023-07-03T17:35:32.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"495151e9","createdTime":"2023-07-03T17:09:49.000+0200","lastRefreshTime":"2023-07-03T17:09:59.000+0200","clientId":"https://myRaspi:11443","scope":"admin"},{"sessionId":"dbc24c32","createdTime":"2023-07-03T17:39:45.000+0200","lastRefreshTime":"2023-07-06T15:29:45.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"3c27a2e5","createdTime":"2023-07-06T15:38:20.000+0200","lastRefreshTime":"2023-07-06T15:44:06.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"12c1a862","createdTime":"2023-07-06T15:46:59.000+0200","lastRefreshTime":"2023-07-06T15:50:32.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"fbc40a17","createdTime":"2023-07-06T16:00:01.000+0200","lastRefreshTime":"2023-07-06T16:00:56.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"dfe6e7b0","createdTime":"2023-07-06T16:05:21.000+0200","lastRefreshTime":"2023-07-07T15:41:01.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"27938f79","createdTime":"2023-07-07T15:42:30.000+0200","lastRefreshTime":"2023-07-07T16:41:16.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"4e4597f9","createdTime":"2023-07-07T17:04:17.000+0200","lastRefreshTime":"2023-07-07T17:04:27.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"ba30191c","createdTime":"2023-07-07T17:05:57.000+0200","lastRefreshTime":"2023-07-08T18:35:48.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"94740e6e","createdTime":"2023-07-08T18:59:38.000+0200","lastRefreshTime":"2023-07-09T10:54:14.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"73e09c74","createdTime":"2023-07-09T08:36:27.000+0200","lastRefreshTime":"2023-07-09T10:15:38.000+0200","clientId":"https://myRaspi:11443","scope":"admin"},{"sessionId":"2ffa9181","createdTime":"2023-07-09T10:38:07.000+0200","lastRefreshTime":"2023-07-09T11:31:50.111+0200","clientId":"https://myRaspi:11443","scope":"admin"},{"sessionId":"ed424ae7","createdTime":"2023-07-09T11:16:33.220+0200","lastRefreshTime":"2023-07-09T12:51:08.993+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"1d1ab403","createdTime":"2023-07-09T11:32:20.869+0200","lastRefreshTime":"2023-07-09T11:32:40.563+0200","clientId":"https://myRaspi:8444","scope":"admin"}]
12:54:35.764 [INFO ] [org.openhab.core.model.script.rules  ] - Bla
12:54:35.767 [INFO ] [org.openhab.core.model.script.rules  ] - sessionId: [{"sessionId":"bf557728","createdTime":"2023-06-13T17:11:56.000+0200","lastRefreshTime":"2023-06-27T17:29:26.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"ab260658","createdTime":"2023-06-21T18:37:11.000+0200","lastRefreshTime":"2023-07-08T23:02:22.000+0200","clientId":"https://192.168.180.33:11443","scope":"admin"},{"sessionId":"1929c769","createdTime":"2023-06-26T18:04:02.000+0200","lastRefreshTime":"2023-06-26T18:05:17.000+0200","clientId":"https://myRaspi:11443","scope":"admin"},{"sessionId":"1d37ea5b","createdTime":"2023-06-27T17:32:54.000+0200","lastRefreshTime":"2023-07-01T16:24:22.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"60c27f8f","createdTime":"2023-07-01T16:28:19.000+0200","lastRefreshTime":"2023-07-01T18:15:55.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"2e4922ed","createdTime":"2023-07-01T17:29:32.000+0200","lastRefreshTime":"2023-07-01T17:29:43.000+0200","clientId":"https://myRaspi:11443","scope":"admin"},{"sessionId":"4d503deb","createdTime":"2023-07-01T18:18:41.000+0200","lastRefreshTime":"2023-07-01T20:40:13.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"eb7721cc","createdTime":"2023-07-01T20:03:09.000+0200","lastRefreshTime":"2023-07-01T20:03:20.000+0200","clientId":"https://myRaspi:11443","scope":"admin"},{"sessionId":"8332f5e6","createdTime":"2023-07-01T20:42:44.000+0200","lastRefreshTime":"2023-07-02T16:33:55.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"8e71361b","createdTime":"2023-07-02T16:08:52.000+0200","lastRefreshTime":"2023-07-02T16:09:02.000+0200","clientId":"https://myRaspi:11443","scope":"admin"},{"sessionId":"c0fca1c5","createdTime":"2023-07-02T16:37:52.000+0200","lastRefreshTime":"2023-07-02T16:43:20.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"fe6b52ef","createdTime":"2023-07-02T16:46:50.000+0200","lastRefreshTime":"2023-07-02T17:22:23.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"29e4e9c5","createdTime":"2023-07-02T17:25:30.000+0200","lastRefreshTime":"2023-07-03T15:45:00.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"db524553","createdTime":"2023-07-03T16:22:05.000+0200","lastRefreshTime":"2023-07-03T17:35:32.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"495151e9","createdTime":"2023-07-03T17:09:49.000+0200","lastRefreshTime":"2023-07-03T17:09:59.000+0200","clientId":"https://myRaspi:11443","scope":"admin"},{"sessionId":"dbc24c32","createdTime":"2023-07-03T17:39:45.000+0200","lastRefreshTime":"2023-07-06T15:29:45.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"3c27a2e5","createdTime":"2023-07-06T15:38:20.000+0200","lastRefreshTime":"2023-07-06T15:44:06.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"12c1a862","createdTime":"2023-07-06T15:46:59.000+0200","lastRefreshTime":"2023-07-06T15:50:32.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"fbc40a17","createdTime":"2023-07-06T16:00:01.000+0200","lastRefreshTime":"2023-07-06T16:00:56.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"dfe6e7b0","createdTime":"2023-07-06T16:05:21.000+0200","lastRefreshTime":"2023-07-07T15:41:01.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"27938f79","createdTime":"2023-07-07T15:42:30.000+0200","lastRefreshTime":"2023-07-07T16:41:16.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"4e4597f9","createdTime":"2023-07-07T17:04:17.000+0200","lastRefreshTime":"2023-07-07T17:04:27.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"ba30191c","createdTime":"2023-07-07T17:05:57.000+0200","lastRefreshTime":"2023-07-08T18:35:48.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"94740e6e","createdTime":"2023-07-08T18:59:38.000+0200","lastRefreshTime":"2023-07-09T10:54:14.000+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"73e09c74","createdTime":"2023-07-09T08:36:27.000+0200","lastRefreshTime":"2023-07-09T10:15:38.000+0200","clientId":"https://myRaspi:11443","scope":"admin"},{"sessionId":"2ffa9181","createdTime":"2023-07-09T10:38:07.000+0200","lastRefreshTime":"2023-07-09T11:31:50.111+0200","clientId":"https://myRaspi:11443","scope":"admin"},{"sessionId":"ed424ae7","createdTime":"2023-07-09T11:16:33.220+0200","lastRefreshTime":"2023-07-09T12:51:08.993+0200","clientId":"http://openhab:8080","scope":"admin"},{"sessionId":"1d1ab403","createdTime":"2023-07-09T11:32:20.869+0200","lastRefreshTime":"2023-07-09T11:32:40.563+0200","clientId":"https://myRaspi:8444","scope":"admin"}]
12:54:35.769 [INFO ] [org.openhab.core.model.script.rules  ] - Blub

However, as you can see, the sessionId does not get parsed, instead the complete output string is returned again. I checked my JSON path expression on https://jsonpath.com/ and there it returns the correct value.

Are there any specifics of transform("JSONPATH" that I’m not aware of? Or what am I doing wrong?

Well, at least for me this code works as expected.

Did you install JSONPATH?

What do you mean by “install”? Probably not :sweat_smile:

Well, you have to install the jsonpath transformation service (UI->Administration->Settings->Other AddOns->Transformation AddOns->JSONPath Transformation->Install.

It’s a module like every other service in openHAB as well…

1 Like