Non JSON string?  [Solved]

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

Moderator: leecollings

Post Reply
Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

Non JSON string?

Post 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
Debian buster on NUC and three RPi with buster.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Non JSON string?

Post 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)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

Re: Non JSON string?

Post 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}
Debian buster on NUC and three RPi with buster.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Non JSON string?

Post 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 ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

Re: Non JSON string?

Post 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
Debian buster on NUC and three RPi with buster.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Non JSON string?  [Solved]

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

Re: Non JSON string?

Post by Number8 »

Brilliant. Thank you
Debian buster on NUC and three RPi with buster.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest