![]() |
|
Help decoding JSON data from socket.http.request - Printable Version +- LogicMachine Forum (https://forum.logicmachine.net) +-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1) +--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8) +--- Thread: Help decoding JSON data from socket.http.request (/showthread.php?tid=1075) |
Help decoding JSON data from socket.http.request - mooselight - 01.11.2017 Dear All, I'm trying to retrieve consumption info from an "Ecocompteur" box from Legrand with the following code, but it does not work as expected. The returned Content-Type is "text/plain" while it should be "application/json", so the json.decode function does not work. Is there another way to retrieve data values from the returned "json" data ? Thanks in advance Code: Accessing "http://192.168.1.xx/inst.json" give this data in return :
{
"data1":27.000000,
"data2":0.000000,
"data3":0.000000,
"data4":0.000000,
"data5":0.000000,
"data6":0.000000,
"data6m3":0.000000,
"data7":0.000000,
"data7m3":0.000000,
"heure":22,
"minute":5
}Code: require 'ltn12'
require 'socket.http'
json = require("json")
local request_url = "http://192.168.1.xx/inst.json"
local response_body = {}
local body, code, hdrs, stat = socket.http.request
{
url = request_url;
sink = ltn12.sink.table(response_body);
}
log (body, code, hdrs, stat)
-- local response_decode = json.decode(table.concat (response_body))
log (response_body)
-- local data1 = response_decode.data1
-- local data2 = response_decode.data2
...Here is the log output : Code: Ecocompteur 01.11.2017 20:33:54
* arg: 1
* number: 1
* arg: 2
* number: 200
* arg: 3
* table:
[server]
* string: lwip/3.0
[content-type]
* string: text/plain
* arg: 4
* string: HTTP/1.0 200 OK
Ecocompteur 01.11.2017 20:33:54
* string: {
"data1":35.000000,
"data2":0.000000,
"data3":1.000000,
"data4":0.000000,
"data5":0.000000,
"data6":0.000000,
"data6m3":0.000000,
"data7":0.000000,
"data7m3":0.000000,
"heure":20,
"minute":33
}HTTP/1.0 200 OK
Server: lwip/3.0
Content-Type: text/plain
{
"data1":35.000000,
"data2":0.000000,
"data3":1.000000,
"data4":0.000000,
"data5":0.000000,
"data6":0.000000,
"data6m3":0.000000,
"data7":0.000000,
"data7m3":0.000000,
"heure":20,
"minute":33
}RE: Help decoding JSON data from socket.http.request - Erwin van der Zwart - 01.11.2017 Hi, Try this: Code: if type(response_body) == 'table' then
data = json.pdecode(table.concat(response_body))
else
data = json.pdecode(response_body)
end
log(data)Erwin RE: Help decoding JSON data from socket.http.request - admin - 02.11.2017 Try running this then post both log entries. What it looks like is that the response is incorrect. JSON functions expect valid input which does not have "Content-Type" set anywhere so it's not the problem. Code: require('json')
require('socket.http')
url = 'http://192.168.1.xx/inst.json'
data = socket.http.request(url)
log(data)
log(json.pdecode(data))RE: Help decoding JSON data from socket.http.request - mooselight - 02.11.2017 (02.11.2017, 06:55)admin Wrote: Try running this then post both log entries. What it looks like is that the response is incorrect. JSON functions expect valid input which does not have "Content-Type" set anywhere so it's not the problem. Here is the log entries I got : Code: Ecocompteur 02.11.2017 07:58:47
* string: {
"data1":195.000000,
"data2":0.000000,
"data3":0.000000,
"data4":173.000000,
"data5":0.000000,
"data6":0.000000,
"data6m3":0.000000,
"data7":0.000000,
"data7m3":0.000000,
"heure":7,
"minute":58
}HTTP/1.0 200 OK
Server: lwip/3.0
Content-Type: text/plain
{
"data1":195.000000,
"data2":0.000000,
"data3":0.000000,
"data4":173.000000,
"data5":0.000000,
"data6":0.000000,
"data6m3":0.000000,
"data7":0.000000,
"data7m3":0.000000,
"heure":7,
"minute":58
}
Ecocompteur 02.11.2017 07:58:47
* arg: 1
* nil
* arg: 2
* string: Expected the end but found invalid token at character 253(I PM you more info) Thx (01.11.2017, 21:50)Erwin van der Zwart Wrote: Hi, Thx Erwin, but it does not work. Here is the log entry : Ecocompteur 02.11.2017 08:33:27 * nil RE: Help decoding JSON data from socket.http.request - admin - 02.11.2017 Strange, I've tested it with your access details and it works correctly. Which firmware version are you running? RE: Help decoding JSON data from socket.http.request - admin - 02.11.2017 Can you also try running request like this? Code: sock = require('socket').tcp()
sock:settimeout(5)
res, err = sock:connect('192.168.1.xx', 80)
if res then
sock:send('GET /inst.json HTTP/1.1\r\n\r\n')
res = sock:receive('*a')
log(res)
if res then
p, e = res:find('\r\n\r\n', 1, true)
if e then
data = res:sub(e + 1)
data = require('json').pdecode(data)
log(data)
end
end
endRE: Help decoding JSON data from socket.http.request - mooselight - 02.11.2017 (02.11.2017, 07:44)admin Wrote: Strange, I've tested it with your access details and it works correctly. Which firmware version are you running? 1Version 2: 20170620 It should be the last I think ? I'll try again .. (02.11.2017, 07:48)admin Wrote: Can you also try running request like this? Thanks, it works perfectly ! |