JsonPath transformation problem

I am trying to get carbon intensity and generation mix from this API: https://api.carbonintensity.org.uk/regional/regionid/6 using the HTTP binding.

My thing has a channel:

 Type number : Intensity [ stateTransformation="JSONPATH:$.data[0].data[0].intensity.forecast", mode="READONLY"]

which works perfectly.

I am now trying to query the JSON array for the percentage of generation by each fuel.

Using $.data[0].data[.generationmix[?(@.fuel == 'gas')].perc in https://jsonpath.com/, I get the correct answer, but in OpenHAB, I get the whole JSON string back. This is expected behaviour if there are no matches.

To try to diagnose the problem, I have shrunk the expression to: $.data[0].data[0].generationmix; In the parser, I get the array of percentage by fuel type but in OpenHAB, I still get the whole string, indicating no match.

Two questions:

  • Is there an online JSON parser that can reliably be used to test expressions prior to using them in OpenHAB?
  • Any pointers as to where my expressions are wrong?


Yes they are found in the doccuments :slight_smile:


Use expressions from Jayaway

You are on the right path.


$.data[0].data[.generationmix[?(@.fuel == 'gas')].perc

If you check your logs what do you see?

I’ve just tried the following Thing:

UID: http:url:CarbonIntensity
label: Carbon Intensity
thingTypeUID: http:url
  authMode: BASIC
  ignoreSSLErrors: true
  baseURL: https://api.carbonintensity.org.uk/regional/regionid/6
  delay: 0
  stateMethod: GET
  refresh: 30
  commandMethod: GET
  contentType: application/json
  timeout: 3000
  bufferSize: 2048
  - id: raw
    channelTypeUID: http:string
    label: Raw
    description: ""
    configuration: {}
  - id: generationmix
    channelTypeUID: http:string
    label: Generation Mix
    description: ""
      mode: READONLY
      stateTransformation: JSONPATH:$.data[0].data[0].generationmix

We can’t see your new Channel, but don’t forget the JSONPATH: start to the transformation.

The Item linked to the second Channel shows NULL, and the following warning in the logs:

JsonPath expressions with more than one result are only supported for Boolean, Number and String data types, please adapt your selector. Result: [{"fuel":"biomass","perc":0.9},{"fuel":"coal","perc":0},{"fuel":"imports","perc":1.2},{"fuel":"gas","perc":54.8},{"fuel":"nuclear","perc":22.3},{"fuel":"other","perc":0},{"fuel":"hydro","perc":14.3},{"fuel":"solar","perc":0.4},{"fuel":"wind","perc":6}]

Both the Channel type and Item type are Strings, so not sure what this error message means…

This gives me the following error in the logs:

Executing transformation ChannelStateTransformation{pattern='$.data[0].data[.generationmix[?(@.fuel == 'gas')].perc', serviceName='JSONPATH'} failed: An error occurred while transforming JSON expression.

…and I think is exactly the same as what OP tried

OK, got it using this:

It was:

$.data[0].data[0].generationmix[?(@.fuel == 'gas')].perc


1 Like