I could use some help understanding how to parse JSON from a HTTP call.
I have a simple program (below) that obtains JSON using the following http call:
http://www.opensmarthouse.org/dmxConnect/api/zwavedatabase/device/list.php?filter=manufacturer:0x000C%204447:3035
The program then tries parsing the result into JSON two ways:
- The first way is to use a string "storedData" which contains a stored version of the HTTP response data that I cut/paste directly from a browser when entering the http call, above.
- The second way is to get the JSON from within groovy using the getHTTP call and processing resp.data.
It should that the two strings are the same. However, when the program processes them, the first "storedData" is converted to JSON successfully, but the second time, when resp.data is used, there's an error. Does anybody understand what is happening here?
When I try to parse the resulting JSON, I get the following error:
metadata {
definition (name: "Super Parameter Tool",namespace: "jvm", author: "jvm") {
command "getParameterInfo"
}
}
void getParameterInfo()
{
log.info "getting Parameter information"
String DeviceInfoURI = "http://www.opensmarthouse.org/dmxConnect/api/zwavedatabase/device/list.php?filter=manufacturer:0x000C%204447:3035"
httpGet([uri:DeviceInfoURI, contentType: "application/json",
textParser:true])
{ resp->
def contentType = resp.headers['content-type']
log.info "Response.data: ${resp.data}"
log.info "response content type is ${contentType}"
def slurper = new groovy.json.JsonSlurper()
// Next two lines set up JsonSlurper and give it a string that consist of the same string contained in resp.data
// This one seems to work
log.debug "Result when parse is provied the resp.data string directly:"
String sameResult = '{"search_filter":{"manufacturer":12,"filter":"4447:3035"},"total":2,"devices":[{"label":"HS-WS200+","description":"Scene Capable Wall Switch","manufacturer_name":"HomeSeer Technologies","id":818,"version_min":"5.007","version_max":"5.007","protocol_version":"4.061","manufacturer_ref":12,"uuid":"hsws200","category":"Battery"},{"label":"HS-WS200+","description":"Scene Capable Wall Switch","manufacturer_name":"HomeSeer Technologies","id":1168,"version_min":"5.012","version_max":"255.255","protocol_version":"4.061","manufacturer_ref":12,"uuid":"hsws200","category":"Battery"}]}'
def result = slurper.parseText(sameResult)
log.debug result
// this one should be using the same data, but it fails.
log.debug "Results when parse is provied the string from the http response resp.data:"
def result2 = slurper.parseText(resp.data)
log.debug "Result directly from HTTP response data: ${result2}"
}
}