Page 1 of 1
JSON - getting data from an HTTP-response
Posted: Tuesday 28 March 2023 23:21
by peterbos
Hi,
If I do a call to
Code: Select all
https://apps.hvcgroep.nl/rest/adressen/1562BP-1
I get this response:
Code: Select all
[{"bagId":"0479200000033698","postcode":"1562BP","huisnummer":1,"huisletter":"","huisnummerToevoeging":"","openbareRuimteNaam":"Glennstraat","woonplaatsNaam":"Krommenie","latitude":52.50839,"longitude":4.755153,"woonplaatsId":1880,"gemeenteId":479}]
I searched the forum and the rest of the internet but can't find how to retrieve the values of the fields as strings (frankly, I miss a manual how to use json in dzVents). Can someone point me in the right direction?
Peter
Re: JSON - getting data from an HTTP-response
Posted: Wednesday 29 March 2023 7:25
by roblom
Not a direct answer to your question, but there is a plugin you can use for that.
https://github.com/Xorfor/Domoticz-Afvalwijzer-Plugin
Re: JSON - getting data from an HTTP-response
Posted: Wednesday 29 March 2023 8:47
by waltervl
And there is also a garbage calender lua script for almost the whole netherlands:
https://github.com/jvanderzande/GarbageCalendar
For example scripts with json search for keyword "isJSON" in this forum:
search.php?keywords=isjson
The parsing of json depends on its structure. As dzvents is lua you can also search outside this forum for lua and json.
Re: JSON - getting data from an HTTP-response
Posted: Wednesday 29 March 2023 10:08
by rwblinn
Please find a dzVents example triggered by a switch (hardware dummy virtual sensor):
Code: Select all
-- Domoticz IDX of the switch triggering HTTP request
local IDX_SWITCH = 16
local URL_SERVER = "https://apps.hvcgroep.nl/rest/adressen/1562BP-1"
local RES_HTTP = "RES_1562BP"
local LOG_MARKER = "LOG_1562BP"
return {
on = {
devices = { IDX_SWITCH },
httpResponses = { RES_HTTP }
},
logging = {
level = domoticz.LOG_INFO, marker = LOG_MARKER,
},
execute = function(domoticz, item)
if (item.isDevice) then
domoticz.openURL({ url = URL_SERVER, method = 'GET', callback = RES_HTTP, })
end
if (item.isHTTPResponse) then
if (item.statusCode == 200) then
if (item.isJSON) then
-- Get the data which is a JSON array with one element
local data = item.json[1]
domoticz.log(string.format('lon=%s, lat=%s', tostring(data["longitude"]), tostring(data["latitude"])))
-- lon=4.755153, lat=52.50839. Instead data["key"] can also use data.key:
-- domoticz.log(string.format('lon=%s, lat=%s', tostring(data.longitude), tostring(data.latitude)))
end
else
-- Error like 7 false; ERROR 7:Couldn't connect to server
domoticz.log(string.format("ERROR %d:%s", item.statusCode, item.statusText))
end
end
end
}
Re: JSON - getting data from an HTTP-response
Posted: Wednesday 29 March 2023 10:12
by willemd
If you create a new DZvents script and choose the template for http request, you can then fill in your api call and modify the assignment of someValue slightly and you will get the example below.
I guess your problem was how to get a certain value out of the long string and I have included in the example the extraction of the postal code. Documentation on this is indeed very poor and in previous scripts I developed I have spent days of trial and error.
In this case the following works:
local someValue = item.json[1].postcode
The important thing is to look closely to the buildup of your response, i.e. the square brackets, the curled brackets, the field identifiers etc.
Code: Select all
return {
on = {
timer = {
'every 1 minutes' -- just an example to trigger the request
},
httpResponses = {
'trigger' -- must match with the callback passed to the openURL command
}
},
logging = {
level = domoticz.LOG_INFO,
marker = 'template',
},
execute = function(domoticz, item)
if (item.isTimer) then
domoticz.openURL({
url = 'https://apps.hvcgroep.nl/rest/adressen/1562BP-1',
method = 'GET',
callback = 'trigger', -- see httpResponses above.
})
end
if (item.isHTTPResponse) then
if (item.ok) then
if (item.isJSON) then
local someValue = item.json[1].postcode -- just an example
-- update some device in Domoticz
domoticz.log('myTextDevice '.. someValue, domoticz.LOG_INFO)
end
else
domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
domoticz.log(item, domoticz.LOG_ERROR)
end
end
end
}
Re: JSON - getting data from an HTTP-response
Posted: Wednesday 29 March 2023 22:27
by peterbos
Thanks all,
It was the [1] that did the trick. That is not in the template for a http request.
Peter
Re: JSON - getting data from an HTTP-response
Posted: Thursday 30 March 2023 14:09
by willemd
Also note there is no dot before the [1]
basically you use a number between brackets, without a dot, in order to get the part between curly brackets
and you use the name, with a dot before it, of a name/value pair to get the value
and this can be nested in many ways
Once you get that logic is becomes easy ...