Logic Machine Forum
Get data from json format!!! - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Get data from json format!!! (/showthread.php?tid=4390)



Get data from json format!!! - phongvucba - 22.11.2022

Hi everyone, I'm having a bit of trouble getting data from this json code!
[ {
"EventID" : "11223344556677889900",
"EventDateTime" : "2012-04-14T13:03:20",
SALTO HAMS……p.321
"EventTime" : "13:03:20",
"EventDateTimeUTC" : "2012-04-14T11:03:20Z",
"OperationID" : 17,
"OperationDescription": "Door opened: key",
"IsExit" : false,
"UserType" : 0,
"UserName" : "John Smith",
"UserGPF3" : "Marketing department",
"DoorName" : "Gym",
"DoorGPF1" : "Leisure area",
} ]
I want to get the information of the "OperationDescription" line to be able to know if the key is open or closed. Specifically will send it to address 1/1/1
So what should I do Sad . I am a newbie, so everything is very new, please help.
I am very thankful but people !


RE: Get data from json format!!! - admin - 22.11.2022

Do you have some documentation on how this streaming protocol works? The string that you've posted is not valid JSON.


RE: Get data from json format!!! - phongvucba - 06.12.2022

Sorry you ! I just found this today, I don't know if it helps? Sad
oh, if it's not a correct structure, is it XML?


RE: Get data from json format!!! - admin - 07.12.2022

Create a resident script with 0 sleep time. In Salto set transport layer mode to UDP, use LM IP as Host name and set port to 9999. Check what you get in Logs tab in LM when an event happens on the Salto side. Keep message format as JSON to check whether JSON is valid or not.
Code:
if not server then
  require('socket')
  server = socket.udp()
  server:setsockname('*', 9999)
  server:settimeout(60)
end

data = server:receive()
if data then
  log(data)
end



RE: Get data from json format!!! - phongvucba - 09.12.2022

hi thank admin !I will try it


RE: Get data from json format!!! - phongvucba - 07.02.2023

Hello ! It's been a while since I texted you. I can only try it now. It outputs the LOG section like this:
---------------------------------
* string: [
{
"EventDateTimeUTC": "2023-02-07T03:33:58Z",
"OperationID": 17,
"OperationDescription": "Door opened (key)",
"UserName": "MASTER",
"DoorName": "201"
}
]
------------------------------
Can you help me? For example, now I want to get the values of the fields in "String" , for example "MASTER" or "OperationID" to assign to an address 1/1/0 ?
Thank you very much !


RE: Get data from json format!!! - admin - 07.02.2023

This will write UserName to 1/1/0 and OperationID to 1/1/1:
Code:
if not server then
  require('socket')
  server = socket.udp()
  server:setsockname('*', 9999)
  server:settimeout(60)
end

data = server:receive()
if data then
  data = require('json').pdecode(data)

  for _, event in ipairs(data) do
    grp.write('1/1/0', event.UserName)
    grp.write('1/1/1', event.OperationID)
  end
end



RE: Get data from json format!!! - phongvucba - 07.02.2023

Oh, thank so much ! Done...
Thank admin very much Smile)