[SOLVED] Trying to transform json weather data from website

Hi

I am trying to transform with a rule JSON data coming from website.
The JSON structure is pretty complex, and I’m a newbie at JSONPATH. I’ve tried test rules / logs and website (http://jsonpath.com/) with little success.

Goal is to retrieve weather forecast daily, and parse results to update items.
For example, I would like to get to get single “temperature : 2m” data.
I’ve tried : $…temperature.2m , which sends an array of all temperature/2m data.
Anything beyond this I’ve failed.

Thanks for any help.

{
   "request_state":200,
   "request_key":"xxx",
   "message":"OK",
   "model_run":"02",
   "source":"internal:GFS:1",
   "2020-09-12 05:00:00":{
      "temperature":{
         "2m":291.7,
         "sol":293.3,
         "500hPa":-0.1,
         "850hPa":-0.1
      },
      "pression":{
         "niveau_de_la_mer":101790
      },
      "pluie":0,
      "pluie_convective":0,
      "humidite":{
         "2m":65.1
      },
      "vent_moyen":{
         "10m":6.7
      },
      "vent_rafales":{
         "10m":8.9
      },
      "vent_direction":{
         "10m":259
      },
      "iso_zero":4005,
      "risque_neige":"non",
      "cape":0,
      "nebulosite":{
         "haute":20,
         "moyenne":0,
         "basse":0,
         "totale":20
      }
   },
   "2020-09-12 11:00:00":{
      "temperature":{
         "2m":297.4,
         "sol":296.4,
         "500hPa":-0.1,
         "850hPa":-0.1
      },
      "pression":{
         "niveau_de_la_mer":101990
      },
      "pluie":0,
      "pluie_convective":0,
      "humidite":{
         "2m":41.8
      },
      "vent_moyen":{
         "10m":10
      },
      "vent_rafales":{
         "10m":12.1
      },
      "vent_direction":{
         "10m":302
      },
      "iso_zero":3980,
      "risque_neige":"non",
      "cape":0,
      "nebulosite":{
         "haute":12,
         "moyenne":0,
         "basse":0,
         "totale":12
      }
   }
}

I don’t know anything about transformation of json on a rule but why dont you just use the weather binding? You can get a free weather forecast for the next 7 days with all data like rain, temperature, wind, condition, … .

I was using openweathermap for now, but values are completely wrong (for temperature for example).
Have not tried weather binding yet (this is v1 binding?).
Forecast.io is now owned by Apple and no API anymore. Other services I have not tried yet.

Retrieving the array from JSONPATH is not best option, however if I find how to extract single value afterwards in the rule, that could be ok. Fighting with doing that too for the moment… :rofl:

Yeah, I had a play with the JSON at the link you posted, and could only ever reduce it down to an array of two values using (I think):

$.*.temperature.2m

So, because I lack the skills, I would then either try to predict the timestamp string which holds the correct 2m value (I presume the times are constant throughout the day?), then extract the value in a rule, or do as you’re doing, and extract the value from the returned ‘array’.

Not ideal though.

I’ve seen @rossko57 work magic with JSONPath before…

No magic, this is a toughy. Just patience and playing in a rule.

Always remember the openHAB JSONPATH transformation service is not JSONPATH, there are limitations (most notably we can only get a single string for a result). The online expression testers are not always helpful here.
When testing, if you break the transform it will return the whole string that you started with.

Which one do you want?
transform("JSONPATH", "$.['2020-09-12 05:00:00'].temperature.2m", rawjson)
should work. Need to take care around the key with spaces etc.

But I suppose you will not know the date and time stuff in advance, but you want to get the first or last one for example.
I’m not sure you can do that in JSONPATH

The best I could manage was to get a list of all *.temperature.2m values
transform("JSONPATH", "$..[?(@..temperature)].temperature.2m", rawjson)
returns the string (note it’s not a real array)
[291.7, 297.4]
which you can process further in your rule

Thanks a lot. I think I should be able to guess time in advance, since this is extracted from weather forecast script which produces always the same date / times of day.
In case I want to go the string way, I will try to find ways to play with strings (have not tried yet).

one last question here: if the transform function does not find a match in the JSON, the entire JSON is returned, which is messy.
Should I use transformRaw instead? I would prefer to be able to catch this and not postUpdate the entire JSON in an item in that case. Thanks.

That’s what its for. It’s also possible to test for input == output.