Hi @Angus_M - this is my driver. It's not the complete code, but as an example it shows how to use the groovy.json jsonSlurper .
Learned a bit more about Groovy and removed a lot of the hard coding. Looks like even attribute definitions can be added programatically. 2021-01-02
Hope this helps:-
import groovy.json.*
metadata {
definition (name: "Virtual JSON Parser", namespace: "timtuxworth", author: "Tim Tuxworth") {
capability "Actuator"
command "sendLockCodes",["string"]
attribute "lockcodeslist", "string"
attribute "lastUpdated", "string"
attribute "slotlabel", "string"
attribute "codelabel", "string"
attribute "namelabel", "string"
Integer s;
for(s = 1; s <= 9; s++)
{
attribute "slot" + Integer.toString(s), "number"
attribute "code" + Integer.toString(s), "string"
attribute "name" + Integer.toString(s), "string"
}
}
def sendLockCodes(lockcodesjson) {
sendEvent(name: "lockcodeslist", value: lockcodesjson, displayed: true)
def jsonSlurper = new JsonSlurper()
def lockobjects = jsonSlurper.parseText(lockcodesjson)
assert lockobjects instanceof Map
sendEvent( name: "slotlabel", value: "#")
sendEvent( name: "codelabel", value: "Code")
sendEvent( name: "namelabel", value: "Name")
Integer s;
for(s = 1; s <=9; s++)
{
sendEvent( name: "slot" + Integer.toString(s), value: Integer.toString(s))
sendEvent( name: "code" + Integer.toString(s), value: "-")
sendEvent( name: "name" + Integer.toString(s), value: "-")
}
// for this to work the JSON passed in must be in the format, by adding attributes you could extract any number of rows.
// {"1":{"name":"Test1","code":"1111"},
// "2":{"name":"Jo2","code":"2121"},
// "3":{"name":"XX","code":"1234"}
// }
log.debug(lockobjects.toString())
lockobjects.each { lockobject ->
def slot = lockobject.key
sendEvent( name: "slot" + slot, value: slot)
sendEvent( name: "code" + slot, value: lockobjects[slot].code)
sendEvent( name: "name" + slot, value: lockobjects[slot].name)
}
lastUpdated = new Date()
sendEvent( name: "lastUpdated", value: lastUpdated.format("MM-dd - h:mm:ss a") )
}