[SOLVED] HTTP binding and JSONPATH: unable to extract data

Dear all,
I do not consider myself an experienced user of OpenHAB, but I am playing with it since a couple of years mainly by adapting samples taken from this same community or elsewhere on the web. I am now asking help because I am trying to extract an information that should be contained inside a JSON structure. I have the following line inside the .items file:

String Migrants "Migrants" {http="<[https://data2.unhcr.org/api/population/get?widget_id=74014&geo_id=656&sv_id=11&population_group=4797&year=latest:60000:JSONPATH($.data.0.individuals)]"}

And the following very elementary line on the sitemap:

Text item=Migrants

Well, actually in my specific case the information is a number, but given that at the moment I am not interested in any further elaboration I can also find acceptable to represent as a string. The issue is that on the sitemap I do not see any value. I am using OpenHAB 2.3 on a Raspberry Pi, and both the HTTP binding and JSONPATH transformation are obviously installed. I also made some tests on http://jsonpath.com/ and apparently the expression “$.data.0.individuals” seems to be correct.
I am pretty convinced that I made some stupid mistake due to my inexperience. Someone of you can help me? Thanks in advance!

All the best, Marco

Have you tried with Text item=Migrants label=“Somename [%d]”
or use [%s] for string?

Hi H102, thank for your answer.
With [%s] for string I can now see the entire JSON output, without any parsing. Or more precisely, I can see the very initial part of the JSON output, given that the remaining is truncated.
At this point I suspect that the issue is inside the JSONPATH expression…

From http://jsonpath.com/ I can see this output:

[
“20129”
]

My intention is, of course, to show that value (“20129”), as string or maybe further also as number.

Thank you again

OK, I found the solution: with JSONPATH($.data[0].individuals) is working now!
BTW, it is working also as a number with this expression:

Number Migrants "Migrants [%d]" {http="<[https://data2.unhcr.org/api/population/get?widget_id=74014&geo_id=656&sv_id=11&population_group=4797&year=latest:60000:JSONPATH($.data[0].individuals)]"}

Thank you! :slightly_smiling_face:

1 Like

Sorry but may I know what this API is out of curiosity?
I’m a sucker for this kind of data

It is an API managed by UNHCR, the Refugee Agency of United Nations that gives detailed statistics about refugee situations all over the world. You can find information here:

http://data.unhcr.org/wiki/index.php/API_Documentation.html

BTW, I am not directly involved in these issues, but the question of immigrations from the Mediterranean is very discussed in my country (Italy) as you probably know, and I want to keep myself informed with reliable data and not with the crap told by some politicians. As a matter of fact I am testing OpenHAB also for a function for which perhaps it was not created, but for which it could work well, i.e. as an “information aggregator”.
APIs can help a lot for this, but my next step will be with RSS feeds. I am going slowly, because I do not have so much spare time, and also because I am almost 58 years old: younger generations are much faster as regards the assimilation of these techniques!! :slight_smile: