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:
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
}
Reply


Messages In This Thread
Help decoding JSON data from socket.http.request - by mooselight - 01.11.2017, 21:13

Forum Jump: