JSON - getting data from an HTTP-response

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
peterbos
Posts: 93
Joined: Saturday 07 November 2020 21:41
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

JSON - getting data from an HTTP-response

Post 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
roblom
Posts: 408
Joined: Wednesday 26 February 2014 15:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: the Netherlands
Contact:

Re: JSON - getting data from an HTTP-response

Post 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
User avatar
waltervl
Posts: 6691
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: JSON - getting data from an HTTP-response

Post 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.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
rwblinn
Posts: 72
Joined: Wednesday 10 June 2015 21:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 4
Location: Hamburg (Germany)/Middelburg (NL)
Contact:

Re: JSON - getting data from an HTTP-response

Post 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
}
Last edited by rwblinn on Wednesday 29 March 2023 10:48, edited 1 time in total.
willemd
Posts: 741
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: JSON - getting data from an HTTP-response

Post 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
}
peterbos
Posts: 93
Joined: Saturday 07 November 2020 21:41
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: JSON - getting data from an HTTP-response

Post by peterbos »

Thanks all,

It was the [1] that did the trick. That is not in the template for a http request.

Peter
willemd
Posts: 741
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: JSON - getting data from an HTTP-response

Post 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 ...
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest