Channel Profile Setting to Count Value Occurrences in JSON String

I’m usually able to find examples that get me to a solution, but this one is frustrating me. I currently have Version 3.2.0.M1 running on an RPi3.

I have a channel returning a string of the form:

{ "items": [
    { "name": "A", "status": "OK"   , "num": 2, ... },
    { "name": "B", "status": "OK"   , "num": 0, ... },
    { "name": "C", "status": "NotOK", "num": 3, ... },
    ... ] }

I was able to use a JSONPATH Profile on a channel Item to get a sum of the num fields:


What I can’t figure out is a Profile to count the number of “NotOK” occurrences. I was hoping it would be as simple as

$.count($..status[?(@ == 'NotOK')])

but, I gather, only max, min, and sum, but not count, are supported.

I also tried playing with a REGEX but that’s even more obscure to me. And I was trying to avoid the complexity of an external Javascript file. Although I’d consider that if there was a way to pass it both the Channel JSON and the status value I want to count because there are several possible values in my case, each of which I’d like to separately count.

In similar manner, I was looking to avoid the complexity of a trigger firing to do the counting. After all, if the sum can be done so simply and transparently, I assume(/hope) there’s a way to count in similar fashion?

There is nothing built into JSONPATH that will give you the count. You have to run the JSONPATH and call .size on the result but to do that you have to do it somewhere you can execute code.

You cannot pass anything extra to the JS transformation. You’ll have to write a separate .js file for each thing you want to count.

Thank you for confirming my suspicion.

But, after posting, I was wondering if I could use the “State Formatter”, revealed by the “Show advanced” toggle, to append something to the JSON that could then be stripped off by the called Javascript before parsing the JSON. What I would append, of course, is the Status value I wanted counted.

Given that the State Description is called last of all that won’t work. Depending on the binding you might be able to chain transformations but the only transformation I know of that will let you append to the value is the JS transform so you may as well just code it all in the one JS transformation. Or move all the processing in a Rule.

There is no “simple” way around this. It’s a limitation of JSONPATH.