Getting JSON from HTTP - Help - bug?

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:

  1. 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.
  2. 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}"
	}
}




It looks like the guys in this thread worked through a similar issue with textParser:true. Maybe this code snippet would help: How do I parse battery percentage from a HTTP GET response?

More directly, this seems to be the dance to get the StringReader into a String for use with JsonSlurper.

def data = "${response.getData()}".toString()

Edit: It would probably also just parse it for you automatically so that you could get at the Json structure in the returned response object like accessing members of a map. Is there another reason that you need to specify textParser:true? If not, what happens if you leave it out (and also skip the JsonSlurper parsing step, since that will then happen under the hood of the http method before the response is returned)?

Yes, that was it. I could have sworn I tried the call without including the textParser:true and without using slurper, but on your suggestion, I tried it again and it worked with a return data type of Map.

Thank you very much!

import groovy.json.JsonSlurper

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])
    { resp->
        log.info "Unparsed Data: ${resp.data}"
        log.info "Response Data class: ${resp.data instanceof Map}"
	}
}
1 Like