opus
(Jürgen Baginski)
February 7, 2017, 6:42pm
1
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?
rtvb
(Robert)
February 7, 2017, 10:23pm
2
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
opus
(Jürgen Baginski)
February 8, 2017, 3:50pm
3
Nope!
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.
rtvb
(Robert)
February 8, 2017, 5:12pm
4
I will try a live example from my tutorial tonight to see if I van get this to work. Like to learn myself
Get local UV radiation -- prevent sunburn and skin cancer
rtvb
(Robert)
February 9, 2017, 5:08am
5
My result of the use of length was ‘null’. I did some googling and read this
java, arrays, json, size
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
opus
(Jürgen Baginski)
February 9, 2017, 5:39am
6
Thanks for your efforts.
That looks like we are on 2.0.
rtvb
(Robert)
February 9, 2017, 6:36am
7
@maintainers What would be the best way to request an upgrade of JSONPATH?
Kai
(Kai Kreuzer)
February 9, 2017, 8:00am
8
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.
carsten
(Carsten Wuthenow)
January 7, 2018, 9:47am
9
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
2 Likes
RolandS
(Roland S)
December 9, 2019, 2:08pm
10
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…
rossko57
(Rossko57)
December 9, 2019, 2:14pm
11
“Directly from what”?
To set an Item state you need either a rule or binding of some kind.
RolandS
(Roland S)
December 9, 2019, 2:40pm
12
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
rossko57
(Rossko57)
December 9, 2019, 5:04pm
13
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.