Extracting python output of dictionary type

Tags: #<Tag:0x00007f616f7403d8> #<Tag:0x00007f616f7402c0> #<Tag:0x00007f616f7401d0>

I need help!
I’m trying to extract data from a python call.

Apparently, the format of the string returned is called “dictionary type” and can look like this:

{'data': 57, 'error': 'NO_ERROR'}

I thought at first that this output was json format but sadly it wasn’t that easy.
I’ve been trying to use regex for this task but I’m far from successful.

One of many attempts:

var String transformedTest = transform("REGEX", "*.(\\d*)*.", test.state.toString)

But all I ever get returned is an empty string. I’ve read a lot of posts discussing regex and the need to escape this and that but I cant get my head around it.
I’m doing this in a rule because I first want to split 3 lines by using a split("\n") operation.

What’s your suggestion in dealing with this kind of string?


Another example of the dictionary type output from the PiJuice software (which I also will try to extract):

{'data': {'battery': 'CHARGING_FROM_5V_IO', 'powerInput5vIo': 'PRESENT', 'isFault': False, 'isButton': False, 'powerInput': 'NOT_PRESENT'}, 'error': 'NO_ERROR'}
  • Platform information:
    • Hardware: Raspberry Pi 3b
    • OS: Raspbian Buster
    • Java Runtime Environment: openjdk version “1.8.0_222”
    • openHAB version: 2.5.3 Release Build

This is json, and you should be able to use the JSONPATH transformation service with it. If it’s not working, check for quotes.

This should work if the JSONPATH addon is installed…

var transformedTest = transform("JSONPATH", "$.data", test.state.toString)

This is another really good use for Jython too, since you’d just bring the data in as a dictionary. I don’t know what PiJuice is, but you may be able to use the package directly in a rule too.

1 Like

@5iver, yes you seem to be correct. This really makes it a lot easier as I can skip regex.

I tested the format in a json validator and it failed so I assumed that the format was a close cousin:


{'data': {'battery': 'CHARGING_FROM_5V_IO', 'powerInput5vIo': 'PRESENT', 'isFault': False, 'isButton': False, 'powerInput': 'NOT_PRESENT'}, 'error': 'NO_ERROR'}

was validated to this:

Error: Parse error on line 1:
{ ‘data’: { 'battery
Expecting ‘STRING’, ‘}’, got ‘undefined’

But it still works.
Thank you!