Sending JSON? data

I am following an example I found where the agent sends data to the device like so:
device.send("settings", { a = fid, b = fap, c = isclick, d = dnb});
and the device reads them like:
agent.on("settings",function(v){ feedid = v.a; apikey = v.b; isclick = v.c.tointeger(); dnbase <- v.d.tointeger(); });
My question is can I create this part { a = fid, b = fap, c = isclick, d = dnb} dynamically? Then I could make the first value sent the number of values to follow so I know on the device side how many variables to read…


You can pass pretty much anything as the data parameter for agent/device.on

If you want to send and read a variable number of parameters, you can use a table (as in this example) or an array:

`//agent side:
var someTable <- {};
someTable[“a”] <- fid;
someTable[“anotherKey”] <- fap;
device.send(“settings”, someTable);

//device code:
agent.on(“settings”, function(data) {
// iterate through each value and do something
foreach(key, value in data) {
server.log(key + ": " + value);

`//agent side:
local settings = [];
device.send(“settings”, settings);

//device code
agent.on(“settings”, function(data) {
// iterate through data:
foreach(setting in data) {
// do something with setting

You can also use JSON to create tables and use . to access members I find it easier, but that’s me :slight_smile:

`// Message obj to SS v2 service
msgObj <- {
“msgLen”: null,
“value”: 0,
“status”: null,
“timestamp”: null,
“msg”: “”,
“mType”: null,
“rssi”: 0,
“vdd”: 0,
“t”: null,
msgObj.value = 22.1;