Yes.
If you want to use the JSONPATH transformation, you’ll probably do one element at a time, because the transformation only returns one string.
Examples
You can get it to return a single string that looks like an array “[blah, bleh]” which you can parse further in your rule (e.g. split on commas) but likely easier to get one at a time.
Complex example doing that
I did JSON parsing in a Javascript rule.
//need below to log to openhab.log file
var logger = Java.type(‘org.slf4j.LoggerFactory’).getLogger(‘org.openhab.rule.’ + ctx.ruleUID);
//below is json parse
var obj = itemRegistry.getItem("BOMdata").getState();
var test = JSON.parse(obj) ;
for (var count = 0; count < 7; count++) {
logger.info('Icon: ' + test.data[count].icon_descriptor);
logger.info('Short text: ' + test.data[count].short_text);
logger.info('Date: ' + test.data[count].date);
logger.info('Max: ' + test.data[count].temp_max);
logger.info('Min: ' + test.data[count].temp_min);
logger.info('Forecast ' + test.data[count].extended_text);
}
also like this:
//parse json data:
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)
@denominator I’m just upgraded my setup from OH 2.4 to OH 3.1. (and added also some new hardware in my house).
I use config files (*.items, *.rules, …) to create my whole configuration.
The JSON data (string) is received by Openhab via one MQTT toppic.
So I don’t think it is possible to create different things/channels for it.
Somehow, somewhere you have also created some kind of Thing for your MQTT data.
The suggestion is to create more MQTT channels (which can all share the same topic) that use different transformations to pick out individual data, and link directly to Items. no rules required.
I didn’t know it was possible to use the transformations when creating the channels. I’m going to take a deeper look into that also.
At this moment I’m testting a rule based on the provided solution of denominator.
Its an example of what you want maybe not the solution as you asked how to do it in a rule.
@rossko57 suggestion is how I would do it in the first place if the OP question was different. You can do it however you like. I would do it way he suggested.
Thanks for the extra examples. Yesterday I figured it out how to to this via the (string) channels. But sometimes the linked items got the value “NULL”. This was caused because not all MQTT JSON strings contains always all devices. This was solved after I added
To handle this you can filter out the messages in the channel that don’t have the device for that channel so you don’t get JSON error in logs. Install the Regex transformation