Help with parsing UDP json response

I’m tyring to parse json response from a UDP request. Let’s say I want the value of the “method” item below. I have the sendUDP function in startupLua. The expected return is in the format:

{“method”:“getSystemConfig”,“env”:“pro”,“result”:{“mac”:“a8bb50891fbe”,“homeId”:654213,“roomId”:990480,“homeLock”:false,“pairingLock”:false,“typeId”:0,“moduleName”:“ESP01_SHTW1C_31”,“fwVersion”:“1.18.0”,“groupId”:0,“drvConf”:[20,1]}}

Here’s the code I’m using:
//code
local JSON = require(“dkjson”)

local WizInfo = (sendUDP(‘{“method”: “getPilot”, “params”:{}}’, “46”))
//
From there, I know I need to parse the json, but that’s where I fall down. I’ve done a lot of searching and reading, am unable to get the items I need. I thank you for your help.

Are you sure of the use of UDP? In you example, you’re just sending out that packet through UDP, there will be no response on/from WizInfo. A TCP or HTTP request would make more sense here. What kind of device are you trying to talk to?

Thanks for the reply. I’m working with Wiz Connected bulbs. Wiz API communicates via UDP and says sending {“method”: “getPilot”, “params”:{}} should get all current settings for given light. If I do sendUDP(‘{“method”: “setPilot”, “params”:{“state”:true}}’, “46”) it turns the light on.
Also, I’m not sure how to see response.

In that case you need to read from an UDP connection, not use just sendUDP. See this example Lua UDP example · GitHub for a bit of code that sends data over UDP and reads from it.

Timeout setting is important, as the default is udp:settimeout(0), which basically means the connection is closed immediately after sending the data. The timeout indicates the connection to remain open for a specified time to be able to read from it.

Then reading should work by:

  data = udp:receive()

Once that is done, you should be able to decode the data using dkjson. This gives the json.decode option, probably something like:

-- set variables

local data
local wizdata = {}

-- receive the data from the bulb
data = udp:receive()

-- decode received data
local jsonresponse = JSON.decode(data)

-- read method field
wizdata.method = jsonresponse.method
wizdata.homelock = jsonresponse.result.homeLock

OMG…thats great info and I totally understand it. I’ll give it a shot and report back. Thanks again, @jouked!!!

@jouked…that worked perfectly. Thanks again.