Page 1 of 1
Non JSON string?
Posted: Friday 01 May 2020 18:51
by Number8
Hello,
isJSON from dzvents returns false for the string herebelow, while this site
https://jsonformatter.curiousconcept.com says it is valid against RFC 8259. Is there someting wrong is this JSON construct? Thank you
Code: Select all
{"id_station":"Saint Pierre de Jards","realtime":{"date_releve_utc":"01.05.2020","heure_releve_utc":"16:43:01","temperature":"15.2"}}
Running dzvents 3.03 on Nuc Debian
Re: Non JSON string?
Posted: Friday 01 May 2020 21:50
by waaren
Number8 wrote: Friday 01 May 2020 18:51
isJSON from dzvents returns false for the string herebelow, while this site
https://jsonformatter.curiousconcept.com says it is valid against RFC 8259. Is there someting wrong is this JSON construct? Thank you
Code: Select all
{"id_station":"Saint Pierre de Jards","realtime":{"date_releve_utc":"01.05.2020","heure_releve_utc":"16:43:01","temperature":"15.2"}}
Running dzvents 3.03 on Nuc Debian
The string is a valid JSON but the site you get it from probably does not fully comply to RFC 4627. In that RFC it is described that a valid JSON response must use the string 'application/json' for the contentType field.
Can you please share what you see if you add :
Code: Select all
dz.log(item,dz.LOG_FORCE) -- if domoticz is first parm of execute function then replace dz with domoticz if you use another varname as item for 2nd parm then replace item with that varname)
you can force dzVents to convert the string to a table by using:
Code: Select all
json = dz.utils.fromJSON(item.data)
Re: Non JSON string?
Posted: Saturday 02 May 2020 8:11
by Number8
You lead me the path, with no definitive solution yet. Since my original post, I modified the html page in order to try to make dzvents understand it is JSON data.
Code: Select all
<html>
<head>
<meta http-equiv="content-type" content="application/json">
</head>
<body>
{"observations":[{"id_station":"Saint Pierre de Jards","realtime":{"date_releve_utc":"[actual_date0_puredate_utc]","heure_releve_utc":"[actual_date0_time_utc]","temperature":[actual_th1_temp_c]}}]}
</body>
</html>
However dzvents does not to read the meta tag, since it is still interpreted as non JSON.
Here is item output
Code: Select all
: ------ Start external script: meteo.lua: HTTPResponse: "meteohubResponse"
2020-05-02 08:02:00.634 Status: dzVents: Info: meteo script: {["headers"]={["Content-type"]="text/html
2020-05-02 08:02:00.634 "}, ["isVariable"]=false, ["statusCode"]=200, ["isJSON"]=false, ["protocol"]="HTTP/1.0", ["ok"]=true, ["isScene"]=false, ["isXML"]=false, ["isCustomEvent"]=false, ["trigger"]="meteohubResponse", ["isHardware"]=false, ["baseType"]="httpResponse", ["_contentType"]="text/html
2020-05-02 08:02:00.634 ", ["isSystem"]=false, ["statusText"]="OK", ["isSecurity"]=false, ["isDevice"]=false, ["data"]="<html>
2020-05-02 08:02:00.634 <head>
2020-05-02 08:02:00.634 <meta http-equiv="content-type" content="application/json">
2020-05-02 08:02:00.634 </head>
2020-05-02 08:02:00.634 <body>
2020-05-02 08:02:00.634 {"observations":[{"id_station":"Saint Pierre de Jards","realtime":{"date_releve_utc":"02.05.2020","heure_releve_utc":"06:02:00","temperature":11.1}}]}
2020-05-02 08:02:00.634 </body>
2020-05-02 08:02:00.634 </html>
2020-05-02 08:02:00.634 ", ["isTimer"]=false, ["callback"]="meteohubResponse", ["isHTTPResponse"]=true, ["isGroup"]=false}
Re: Non JSON string?
Posted: Saturday 02 May 2020 9:49
by waaren
Number8 wrote: Saturday 02 May 2020 8:11
You lead me the path, with no definitive solution yet. Since my original post, I modified the html page in order to try to make dzvents understand it is JSON data.
Your HTTPResponse is not telling domoticz / dzVents that it's response is json and your data is a mix of xml and json. If the site does intend to send a json it does not comply with the standard.
In order to allow dzVents to automatically or forced convert the item.data to a Lua table it must be a valid json
Can you share the link to the url you use ?
Re: Non JSON string?
Posted: Saturday 02 May 2020 10:15
by Number8
your data is a mix of xml and json
Don't understand that. HTML yes, but XML? I may not understand how to build a page that tells the API that the data is JSON
Your HTTPResponse is not telling domoticz / dzVents that it's response is json and your data is a mix of xml and json. If the site does intend to send a json it does not comply with the standard.
Could you kindly let me know how the page should be constructed? I can put anything I want in the page. However I have no mean to modify the web server itself
Can you share the link to the url you use ?
I'm afraid no, this is an internal web site that is not open to internet
Re: Non JSON string? [Solved]
Posted: Saturday 02 May 2020 13:08
by waaren
Number8 wrote: Saturday 02 May 2020 10:15... I may not understand how to build a page that tells the API that the data is JSON
OK understand now you are picking data from a webpage and not from the underlying app.
You first have to filter out the JSON string from the result before you can convert the json to a Lua table
Code: Select all
local body = item.data:match('%b{}') -- extract part of item.data that is between {} ( including the outer {} )
local rt = dz.utils.fromJSON(body) -- convert the json string to rt ( Lua table )
dz.utils.dumpTable(rt) -- dumpt the contents of the table in your log
Re: Non JSON string?
Posted: Saturday 02 May 2020 15:58
by Number8
Brilliant. Thank you