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?

Thanks

Yes they are found in the doccuments :slight_smile:

https://jsonpath.herokuapp.com/

Use expressions from Jayaway

You are on the right path.

Try

$.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
configuration:
  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
channels:
  - id: raw
    channelTypeUID: http:string
    label: Raw
    description: ""
    configuration: {}
  - id: generationmix
    channelTypeUID: http:string
    label: Generation Mix
    description: ""
    configuration:
      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

Thanks

1 Like

Hi all, I have the same problem more or less, I’m trying to get the hourly electricity prices of day from this API:
https://apidatos.ree.es/en/datos/mercados/precios-mercados-tiempo-real?start_date=2023-11-09T00:00&end_date=202 3-11-09T23:59&time_trunc=hour&geo_trunc=electric_system&geo_limit=peninsular&geo_ids=8741
Using this JsonPath expression: $..values.* in https://jsonpath.com/ to the API’s response I got the “values” array, but using the same as a JsonPath transformation:

var Serie_Dia_PVPC = transform("JSONPATH", "$..values.*", Serie_PVPCHoy)

I got this:

2023-11-10 19:38:05.737 [WARN ] [ternal.JSonPathTransformationService] - JsonPath expressions with more than one result are only supported for Boolean, Number and String data types, please adapt your selector.
 Result: [{"value":102.55,"percentage":1,"datetime":"2023-11-10T00:00:00.000+01:00"},{"value":97.9,"percentage":1,"datetime":"2023-11-10T01:00:00.000+01:00"},{"value":91.51,"percentage":1,"datetime":"2023-11-1
0T02:00:00.000+01:00"},{"value":83.76,"percentage":1,"datetime":"2023-11-10T03:00:00.000+01:00"},{"value":83.02,"percentage":1,"datetime":"2023-11-10T04:00:00.000+01:00"},{"value":87.96,"percentage":1,"datetim
e":"2023-11-10T05:00:00.000+01:00"},{"value":80.63,"percentage":1,"datetime":"2023-11-10T06:00:00.000+01:00"},{"value":96,"percentage":1,"datetime":"2023-11-10T07:00:00.000+01:00"},{"value":131.94,"percentage"
:1,"datetime":"2023-11-10T08:00:00.000+01:00"},{"value":108.88,"percentage":1,"datetime":"2023-11-10T09:00:00.000+01:00"},{"value":132.29,"percentage":1,"datetime":"2023-11-10T10:00:00.000+01:00"},{"value":125
.84,"percentage":1,"datetime":"2023-11-10T11:00:00.000+01:00"},{"value":124.02,"percentage":1,"datetime":"2023-11-10T12:00:00.000+01:00"},{"value":118.22,"percentage":1,"datetime":"2023-11-10T13:00:00.000+01:0
0"},{"value":72.38,"percentage":1,"datetime":"2023-11-10T14:00:00.000+01:00"},{"value":86.84,"percentage":1,"datetime":"2023-11-10T15:00:00.000+01:00"},{"value":101.5,"percentage":1,"datetime":"2023-11-10T16:0
0:00.000+01:00"},{"value":120.3,"percentage":1,"datetime":"2023-11-10T17:00:00.000+01:00"},{"value":184.91,"percentage":1,"datetime":"2023-11-10T18:00:00.000+01:00"},{"value":206.65,"percentage":1,"datetime":"
2023-11-10T19:00:00.000+01:00"},{"value":214.07,"percentage":1,"datetime":"2023-11-10T20:00:00.000+01:00"},{"value":187.73,"percentage":1,"datetime":"2023-11-10T21:00:00.000+01:00"},{"value":124.39,"percentage
":1,"datetime":"2023-11-10T22:00:00.000+01:00"},{"value":108.03,"percentage":1,"datetime":"2023-11-10T23:00:00.000+01:00"}]
2023-11-10 19:38:05.752 [INFO ] [b.core.model.script.Serie_PVPC.rules] - >-- Serie PVPC de REE de hoy Serie_Dia_PVPC = NULL

Any thoughts?