How can I pick a value out of a JSON String

  • Platform information: Pi4 Habian 2.5.3

I get updates from Blue Iris (Cameras) each time the profile is changed. I was able use a REST item to display the setting on the HABPanel. However, like a few other devices the OH server gets resarted from time to time and I’d like to have OH request the profile setting on startup.

rule "Startup"
when
//    testing assistant - this will be replaced with on start
    Time cron "0 0/1 * 1/1 * ? *"
then
    biProfileReturn_json.postUpdate(sendHttpGetRequest("http://192.168.1.6:31793/admin?&user=xyw&pw=123"))
    val newValue = transform("JSONPATH", "signal.profile", biProfileReturn_json.state.toString)
    //post the new value to the Number Item
    biProfile.postUpdate( newValue )
    logInfo("Bi", "one more minute")
end

The Get returns
signal=green
profile=6
lock=1

I want to pick that 6 or whatever number is returned out and replace biProfile

I also see in the doc https://www.openhab.org/addons/transformations/jsonpath/ that If the JsonPath expression provided results in no matches, the transformation will return the entire original JSON string which is my case.

I did also try

val newValue = transform("JSONPATH", "signal.profile.lock", biProfileReturn_json.state.toString)

Thanks for a pointer to learn about JSON or a quick fix

That’s not JSON, it’s just key-value pairs.

I think you could extract your value with REGEX, but I don’t know how exactly.

Thanks - I just read the log literally instead of looking at it. The error specifically say JSONPATH not available. I thought o-yah I recall regex was an add on and sure enough it looks like all transformations are not loaded by defualt.

Yep, you would have to install JSONPATH. It helps to share error messages.

It still won’t work on nearly-JSON though :wink:

Yes - It may just not be the day to do this. Maybe tomorrow!
Thank you for the help

For the line in your rule, try this:

    biProfileReturn_json.postUpdate(sendHttpGetRequest("http://192.168.1.6:31793/admin?&user=xyw&pw=123:60000:REGEX((.*))"))

I had add the “:60000:REGEX((.*))” to the transform for a very long JSON file to pull out the one piece of data that I was looking for. Hope this helps!

~John

Thanks John - I did a little playing around and ended up with val newval = transform(“REGEX”, “.profile=(\d).”, biProfileReturnstr.state.toString). In my case the string was within whatever the standard length that regex cosiders and the profile= made it easy to define. Hopefully I remember that option when I encounter a really long string. That isn’t something I would have expected to need to do but appreciate knowing