This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Help decoding JSON data from socket.http.request
#1
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:
123456789101112131415
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:
12345678910111213141516171819202122
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:
123456789101112131415161718192021222324252627282930313233343536373839404142434445
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 }
Reply
#2
Hi,

Try  this:
Code:
123456
if type(response_body) == 'table' then   data = json.pdecode(table.concat(response_body)) else   data = json.pdecode(response_body) end log(data)
BR,

Erwin
Reply
#3
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:
12345678
require('json') require('socket.http') url = 'http://192.168.1.xx/inst.json' data = socket.http.request(url) log(data) log(json.pdecode(data))
Reply
#4
(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.

Code:
12345678
require('json') require('socket.http') url = 'http://192.168.1.xx/inst.json' data = socket.http.request(url) log(data) log(json.pdecode(data))

Here is the log entries I got :

Code:
1234567891011121314151617181920212223242526272829303132333435
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,

Try  this:
Code:
123456
if type(response_body) == 'table' then   data = json.pdecode(table.concat(response_body)) else   data = json.pdecode(response_body) end log(data)
BR,

Erwin

Thx Erwin, but it does not work.
Here is the log entry :

Ecocompteur 02.11.2017 08:33:27
* nil
Reply
#5
Strange, I've tested it with your access details and it works correctly. Which firmware version are you running?
Reply
#6
Can you also try running request like this?

Code:
123456789101112131415161718
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 end
Reply
#7
(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?

Code:
123456789101112131415161718
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 end

Thanks, it works perfectly !
Reply


Forum Jump: