Need help with JSON Path Transformation

Hi,

I need help regarding a JSON Path Transformation, as I would like to monitor the level of birch pollen flying around my location. As there’s no API on the page I’d like to querry, I’m using ParseHub to extract data.
This is working and returns the below JSON response:

{
 "Birke": [
  {
   "Tag": "Montag, 07. Mai",
   "selection2": [
    {
     "Belastung": "1"
    },
    {
     "Belastung": ""
    },
    {
     "Belastung": ""
    },
    {
     "Belastung": ""
    },
    {
     "Belastung": ""
    },
    {
     "Belastung": ""
    }
   ]
  },
  {
   "Tag": "Dienstag, 08. Mai",
   "selection2": [
    {
     "Belastung": "1"
    },
    {
     "Belastung": ""
    },
    {
     "Belastung": ""
    },
    {
     "Belastung": ""
    },
    {
     "Belastung": ""
    },
    {
     "Belastung": ""
    }
   ]
  },
  {
   "Tag": "Mittwoch, 09. Mai",
   "selection2": [
    {
     "Belastung": "1"
    },
    {
     "Belastung": ""
    },
    {
     "Belastung": ""
    },
    {
     "Belastung": ""
    },
    {
     "Belastung": ""
    },
    {
     "Belastung": ""
    }
   ]
  }
 ]
}
  • I added an entry to the http.cfg:
pollen.url=https://www.parsehub.com/api/v2/projects/xxx/last_ready_run/data?api_key=yyy&format=JSON
pollen.updateInterval=3600000

trying to call off the Pollen exposure for Monday 07th of Mai using this item configuration:

Number PollenBirke_d0 "Birkenpollenbelastung heute: [%s]" (Wetter) { http="<[pollen:60000:JSONPATH($.Birke[0].selection2[0].Belastung)]"}

I would have thought to get “1” in response of the above call, but what I see in the logs is:

2018-05-07 12:31:48.266 [WARN ] [ab.binding.http.internal.HttpBinding] - Transformation 'JSONPATH($['Birke'][0]['selection2'][0]['Belastung'])' threw an exception. [response=����ZtKVgobDhMP8F��RPr�,�NU�R��RP�b���t _�7?�$1]G��\O�71SIa,Y����\���g���rJ�I,.)��6T����V2�T��X.�R��p�L�+����H
�̒����
`XX����R����3]
org.openhab.core.transform.TransformationException: Invalid path '$['Birke'][0]['selection2'][0]['Belastung']' in '����ZtKVgobDhMP8F��RPr�,�NU�R��RP�b���t _�7?�$1]G��\O�71SIa,Y����\���g���rJ�I,.)��6T����V2�T��X.�R��p�L�+����H
�̒����
`XX����R����3'
	at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:67) [220:org.openhab.core.compat1x:2.3.0.201803231044]
	at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:194) [215:org.openhab.binding.http:1.12.0.201803230213]
	at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:144) [220:org.openhab.core.compat1x:2.3.0.201803231044]
	at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:166) [220:org.openhab.core.compat1x:2.3.0.201803231044]

What am I doing wrong here? Any help would be highly appreciated.

thanks,
Kurt

Hello Kurt,

if you are familiar with German, you may have a look at this community:

Regards,
Andreas

Hi,

yeah thanks for sharing.
This is actually where I started from, trying to get inspired from some already working solution.
However, the XML Transformation drove me nuts, as I found it to be much more complex, than a JSON call. ( at least I thought so initially :wink: )

Kurt

The response from the http call is:
����ZtKVgobDhMP8F��RPr�,�NU�R��RP�b���t _�7?�$1]G��\O�71SIa,Y����\���g���rJ�I,.)��6T����V2�T��X.�R��p�L�+����H �̒����XX����R����3]`
Not the JSON string
Your JSONPATH is correct and will work on a valid json.
Have you checked your url?

Hi,

thanks for your help and pointing me to the right direction.
I should have spent more time with reading the parsehub docu, as the content encoding of the calls are returned in gzip format, hence the unexpected response.
For lack of a better solution, I’m saving the file locally now, unzipping it and point the JSONPATH to the file, which delivers the expected output. :ok_hand:

Kurt

You could write a script that calls the url
Write the content to a file
Unzip the content and returns the json

Then you could use the exec binding with a JSONPATH transform to get your value

have you tried it without the &format=JSON?
i use parsehub too without the &format=JSON and i get a non gziped json that i can use JSONPATH in a rule with, also if you don’t have it already you will need a something like this rule to trigger a run automatically, to get the new data from the orignal website

rule "trigger parsehub run"
when
  Time cron "0 0 6 ? * * *" // run each day at 6:00 am
then
  sendHttpPostRequest("https://www.parsehub.com/api/v2/projects/<projectkey>/run?api_key=<api_key>")
  logWarn("myLog", "parsehub run started")
end

From the parsehub api reference:

Aha, strange - I get this one in the docu :slight_smile:

@mueslee Thanks for the hint! I didn’t try it without the &format parameter. Yes, I’ve realized a similar rule, only using executeCommandLine to fetch the data from the API, only without a script as @vzorglub suggested. But most importantly: Thanks for the guidance to initiate a new run before fetching the data! I was missing this one. (need to RTM…)

Kurt