Little devil app key value pair help

I am having trouble parsing the key value pair from the little devil app.

I have named one of the slider switches at pin9.

local data = http.jsondecode(request.body); server.log(request.body);

returns this from the little devil switch when turned to ON
2014-10-02 17:57:42 UTC-5 [Agent] {“pin9”:“1”}
And this when turned OFF
2014-10-02 17:57:47 UTC-5 [Agent] {“pin9”:“0”}

I first want to classify the KEY as specifically pin9 then write the value to pin 9 (in device code)

Nothing I have tried seems to work. I can write a conditional for the ‘pin9’ but not the ‘1’ or ‘0’.
I have also tried to slice the value out of it, but that does not seem to work either. Am I supposed to be counting the quotes " as characters for slice?

I have a work around which is to use the round buttons with different key names and search for them, but I think there should be a way to do this… I’m just not skilled enough at squirrel to figure it out!

Help appreciated!

The “1” and the “0” are strings; are you treating them as integers? The data variable holds a table containing a single key-value pair. The key is the string “pin9”, the value is either the string “1” or the string “0”.

Sounds like you want something like:

if ("pin9" in data) { local value = data.pin9.tointeger() device.send("pin9message", value) }

and you’ll need in your device code

agent.on("pin9message", function(value){ hardware.pin9.write(value) })

This assumes you’ve already configured Pin 9 as a DIGITAL_OUT

Thanks smittytone. The code worked perfectly.

Can you explain to me why that worked?

if data contains this “pin9”:“1”

why does tointeger only pick out the 1? Shouldn’t actually post that it cannot convert the string because of the ‘pin9’ in there?

No, “pin9” is the key or table index and “1” is the value assigned to that table slot. This can be more fully explored in JSON syntax references. Perhaps there should be such a refference in the Electric Imp Dev Center.

so tointeger knows to convert the value in the table and since there is only one key/value pair in the table we are all good.

I think I get it, but at some point I will have to see what it looks like to pass a bunch of key value pairs at the same time.

Thanks everyone

tointeger() looks at the string and if the string contains numeric characters and only numeric characters, the function generates the equivalent integer value. Otherwise you get an error.

local string = "42" server.log(string.tointeger()) // Displays '42' in the log string = "test" server.log(string.tointeger()) // Generates error message in the log

So you know, data.pin9.tointeger() basically says “get the value of the property ‘pin9’ of the object ‘data’ and return the integer equivalent of that value if it’s a numeric string”. Squirrel can see data is a table and therefore expects pin9 to be a key within that table and gets the key’s value to pass on to tointeger().

Awesome smittytone, thats the explanation I was looking for…