
A rainy/cold day is a good reason for some experiments .......
Started to make a lua-script (borrowing ideas and scripts from internet), and found a solution for the indicated step 1b), but now stuck at step 2):
the conversion by the borrowed script from JSON to XML produces a file which (
euphemistically) does not meet the definitions for an XML-file.
The script is below, together with 2 related extracts from Domoticz's log.
Any ideas how to proceed?
Applicable, working examples?
Code: Select all
-- =Lua script= Transfer of Info from Domoticz to another computer
-- Original script from StackOverflow, adapted/extended by Toulon7559, version 0.2 for Domoticz@RPI3
-- ===============================================================================================
-- Script-objectives:
-- 1) Make a JSON-call to a source computer at LAN
-- 2) Extract contents and put in JSON-file
-- 3) Convert JSON-file to XML-file
-- 4) Upload JSON-file and/or XML-file to destination computer at LAN or at Internet
-- Line 10 Make JSON-Call to source computer, Extract contents and make JSON-file
J='curl "http://192.168.1.4:8080/json.htm?type=devices&rid=89"'
print('Curl&JSON-call: '.. J)
local JSON_Light1=assert(io.popen(J))
local Contents1 = JSON_Light1:read('*all')
JSON_Light1:close()
print('JSON-file:'..Contents1) -- Check contents of JSON-file
-- Line 18 Convert JSON-file to XML-file
X=Contents1:gsub('{','<ITEM>\n'):gsub('},?','</ITEM>\n')
X=X:gsub('"(%w-)"%s*:%s*(%b[])',
function (k,v)
return string.format("<%s>%s</%s>\n",k,v:sub(2,-2),k)
end)
X=X:gsub('"(%w-)"%s*:%s*"(.-)",?',
function (k,v)
return string.format("<%s>%s</%s>\n",k,v,k)
end)
print ('XML-file: '.. X) -- Check contents of XML-file
-- Line 30 Upload JSON-/XML-file to destination computer by URL-call
-- Upload_URL = to be defined/inserted
commandArray = {}
--remove -- before the line below to actually upload
--commandArray['OpenURL']= Upload_URL
return commandArray

- Print of JSON-file
- screenshot-Lua_JSONCall_File.png (26.41 KiB) Viewed 3690 times
The second screenshot proceeds where the first one ends, and shows the result of the XML-conversion according to the script:
the elements can be recognized, but the required brackets and all headings are missing.
How to add those brackets and headers to the XML-output?

- Print of 'XML-file'
- screenshot-Lua_JSON_to_XML.png (20.71 KiB) Viewed 3690 times

And then obviously the final section of the script needs to be filled with some small & reliable scriptlines ....