I am using OH 4.2.2 and try to figure the http binding out (earlier I simply glued everything with bash code and the OH API which works but is hard to maintain).
If your response doesn’t actually have brackets around that list then it’s not properly formed json and the JSONpath transform might have trouble actually processing it. You would either have to find a way to manually add the brackets consistently, or use some other method.
If you can get it into a form that the JSONpath transformation will recognize, then you can read up on using JSONpath in the docs for that transform.
If that really is the full form of the response, however, then I would probably just go straight to a js script transform. In the script it would only be a few lines to put the brackets around the string and then parse it into an actual JSON array. Then you could search the array for the key of your choice and return the value. Because script transforms can accept a html-like query syntax for passing values, you only need to create this one transform and you can use for any one of those values that you want by passing in the array string.
The script might look like this:
(function(data) {
var objArray = JSON.parse(`[` + data + ']')
var extractObj = objArray.find( ob => { ob.key == inKey })
return extractObj.value
})(input)
Then you would call the script in the transform with the inKey value. For example for the locked parameter:
Oh, sorry, I simply left the brackets out when I copied the example. I just modified my post to reflect that. However, do I need a function anyway ? And where am I supposed to put this function ?
You don’t nee
d the function in that case (although you could still use it, just without the extra brackets in the parse method). You can use the JSONpath transformation:
For this case, your JSONpath would be pretty simple. Something like $[2][‘value’] would retrieve your door locked value. But the doc page has some helpful links at the bottom for basics on JSONpath expressions.
Transforms can be applies in a couple different ways. The basic transform help is a good place to start:
With the http binding, your two best choices are to set the transform at the channel level (in this case because it is incoming information you would want the state transform and not the command transform). In this case you would set up one channel with a slightly different transform for each item of information you wanted to capture. This would probably be the choice most user would take.
The other option is to have just a single channel that returns that complete JSON and then link three different items to that one channel. In that case you would apply the transform to each link so that each item took the general input and extracted a different result from it.
Thank you for your explanation. It is not clear to me what I am to put into the state transform field since the identifier value is the same for every value I receive. I sort of understood how the function you provided works. That’s why I guessed that I might have to use this function to address the particular value I am interested in.
This is where the links at the bottom of the JSONpath doc page will help you out. One of those will take you to the JSONpath library that the transform uses which will show you numerous example of using JSONpath there’s also a link to a basic tutorial on JSON in general which will help you understand the structure you are trying to traverse.
The square brackets give you an array of objects. So, you need to select the array element of interest before you select the value key from that object. That’s what the [2] is in the example I provided (javascript/json arrays are 0-indexed, so [2] is the third element and then you get the value stored in that elements value key.
If the returned json is not always that consistent (if the elements are in a random order or there are a variable number of elements) then you will need to result to some of the JSONpath tests. This is, in fact, the power of JSONpath, that you can build in exactly this sort of test.
The JSONpath transform is not installed by default, so first make sure you have added it from the add-on store. Then if you want to you can create the transform in the UI’s transform page (it will then tell you exactly the identifier to paste into the channel’s transform field). But this one is straightforward enough that you really only need to just have JSONPATH:your_path_here in the transform field you are using.