HowTo to get the number of array-items in a JSON string?

Tags: #<Tag:0x00007f617e1b04e0> #<Tag:0x00007f617e1b0288> #<Tag:0x00007f617e1b0170>

In order to parse each element of an array within a JSON-string I need know its length.
I tried it with:

var int ArrayLength=transform(“JSONPATH”, “$.sub1.sub2.length”, jsonString)

but that didn’t work. Does anybody have a better clue?
Or is it possible to read in each element of thre JSON-string in another way?

This site returns a string as a result for length.

Perhaps you should charge the type with somerhing like this?

var int ArrayLength=transform("JSONPATH", "$.sub1.sub2.length", jsonString).intValue

This one returns:
16:49:53.221 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule TTT: An error occured during the script execution: The name '<XFeatureCallImplCustom>.intValue' cannot be resolved to an item or type.

I will try a live example from my tutorial tonight to see if I van get this to work. Like to learn myself :wink:

Get local UV radiation -- prevent sunburn and skin cancer

My result of the use of length was ‘null’. I did some googling and read this

The use of ‘length’ requires at least jsonpath 2.1.

Can we see what version is used for OH2?

> bundle:list
182 | Active   |  80 | 2.0.0                 | json-path

Thanks for your efforts.
That looks like we are on 2.0.

@maintainers What would be the best way to request an upgrade of JSONPATH?

Best to create an issue at ESH and a PR that changes this line. We will then need to ask the Eclipse IP team to do a check and approve the new version as it is external code.

in Openhab 2.2 jsonpath is upgraded to 2.1 - it works now to determine the length of an array, using the following syntax: var String test = transform(“JSONPATH”, “$.items.length()”, json) --> you need to add the brackets otherwise you get an error - cost me some hours to find it out to I’d like to share :slight_smile:

Is there any way to get the number of items in a JSON array directly in an item definition?
I didn’t get it running with current openHAB2.4

And a rule is no workaround - I need HTTP Authorization which seems not to work in a rule according to several topics here…

“Directly from what”?
To set an Item state you need either a rule or binding of some kind.

directly means in an item definition with the JSONPATH transformation.

Without any rule or proxy item something like this - but it doesn’t work most probably because of the ()

String MyItem "MyItem [%s]" {http="<[MyHTTPURL:60000:JSONPATH('$.workItems.length()')]"}

1 Like

Okay, transform in the binding link then.
Yes, I expect it’s just being picky about format.

A workaround is to use a javascript transform instead - which can include JSONPATH as part of its processing.