Help with JSON Path - Amber Electric

  • Platform information:
    • Hardware: IBM Thinkpad - i5/8gbRAM/128gb SSD
    • OS: Debian
    • Java Runtime Environment: openjdk 11.0.10
    • openHAB version: 3.0.1
  • Issue of the topic: Hi all. Thank you in advance for any help. I am trying to pull in data from Amber Electric in Australia using their API. I have managed to get the JSON string into openhab, but I am having trouble transforming it into usable data. I think there is a problem with the syntax in the transform function, but I cannot work out exactly how to fix it. The item “AmberString” ends up with the entire JSON inside it. I believe it might also be possible to select which data is needed by searching on the “channelType” value, but I cannot quite figure this one out either. Any help is appreciated. Thanks again.

My Rule DSL.

	var String JSON_Current = executeCommandLine(Duration.ofSeconds(5), "/home/luke/scripts/amber.sh")
	val String duration = transform("JSONPATH", "$.0.duration", JSON_Current)
    logInfo("AmberAPI", duration)
    AmberString.postUpdate(duration)

The JSON string that comes in.

[
  {
    "type": "CurrentInterval",
    "date": "2022-04-03",
    "duration": 30,
    "startTime": "2022-04-03T11:30:01Z",
    "endTime": "2022-04-03T12:00:00Z",
    "nemTime": "2022-04-03T22:00:00+10:00",
    "perKwh": 49.26995,
    "renewables": 25.355,
    "spotPerKwh": 27.79159,
    "channelType": "general",
    "spikeStatus": "none",
    "descriptor": "neutral",
    "estimate": true
  },
  {
    "type": "CurrentInterval",
    "date": "2022-04-03",
    "duration": 30,
    "startTime": "2022-04-03T11:30:01Z",
    "endTime": "2022-04-03T12:00:00Z",
    "nemTime": "2022-04-03T22:00:00+10:00",
    "perKwh": 40.78895,
    "renewables": 25.355,
    "spotPerKwh": 27.79159,
    "channelType": "controlledLoad",
    "spikeStatus": "none",
    "descriptor": "high",
    "estimate": true
  },
  {
    "type": "CurrentInterval",
    "date": "2022-04-03",
    "duration": 30,
    "startTime": "2022-04-03T11:30:01Z",
    "endTime": "2022-04-03T12:00:00Z",
    "nemTime": "2022-04-03T22:00:00+10:00",
    "perKwh": -68.22956,
    "renewables": 25.355,
    "spotPerKwh": 27.79159,
    "channelType": "feedIn",
    "spikeStatus": "none",
    "descriptor": "high",
    "estimate": true
  }
]

Welcome to openHAB!

What is the result of this?

Do you have the JSONPATH Transformation Service installed? It isn’t by default.

What happens if you change

to

var String duration

?

What happens if you change

to

$.[0].duration

?

Hey, thanks for the quick response. No I did not have the transformation service installed :crazy_face: I tried your suggestions and found the below now returns the result of “30” in both the log and string item.

val duration = transform("JSONPATH", "$.duration", JSON_Current)

Thank you, I’m very close now. I believe this will be the value from only the first section of the array. I tried:

$.[0].duration
$.0.duration
0.duration

but these still all returned the full string length.

I got it, I had to add an extra set of brackets, then I could access each section correctly:

     JSON_Current = "[" + JSON_Current + "]"
     var String duration = transform("JSONPATH", "$.[0].channelType", JSON_Current)

Thank you again for your help. For some reason, the pasted JSON output above was missing the opening and closing brackets in my string. Adding them back in has solved it.

Good to know you fixed it.

I use https://jsonpath.com/ to test the expressions before I put them into the TRANSFORM function. Paste the response from the server into the box on the left and test expressions in the JSONpath field until you get the results you want.