Help on Parsing JSON response

Here’s the response from webserver received by Device Handler:

482768ca-b09a-497c-b997-a4b51f720c45 3:55:13 PM: debug JSON: [“Door2Vld”:0, “Door1Vld”:1, “Door0Vld”:1]

My DH Code:

def parse(String description) {
log.debug “Parsing ‘${description}’”

def msg = parseLanMessage(description)
def headersAsString = msg.header // => headers as a string
def headerMap = msg.headers      // => headers as a Map
def body = msg.body              // => request body as a string
def status = msg.status          // => http status code of the response
def json = msg.json              // => any JSON included in response body, as a data structure of lists and maps
def xml = msg.xml                // => any XML included in response body, as a document tree structure
def data = msg.data              // => either JSON or XML in response body (whichever is specified by content-type header in response)

log.debug "Receiving Message From Device: " + status + " body: " + body
log.debug "JSON: " + json;

def parseResult = JsonSlurper.parseText(json)            //<<--- Error message point to this line

log.debug "Door0Vld: " + parseResult.Door0Vld 
log.debug "Door1Vld: " + parseResult.Door1Vld 
log.debug "Door2Vld: " + parseResult.Door2Vld 

I’m Getting the following error message:

482768ca-b09a-497c-b997-a4b51f720c45 3:55:13 PM: error groovy.lang.MissingMethodException: No signature of method: static groovy.json.JsonSlurper.parseText() is applicable for argument types: (java.util.HashMap) values: [[“Door2Vld”:0, “Door1Vld”:1, “Door0Vld”:1]]

Am I missing something? TIA!!

The issue appears to be that what you are getting from the webserver isn’t actually JSON so it cannot parse it with JsonSlurper. I have had similar issues with my projects as well so I am hopeful that one of the guys who does a lot of this can provide some insight. For me passing valid JSON back and forth sounds simple but has been a challenge at times. I look forward to the expert responses.

1 Like

The msg.json is already parsed, no need to call parseText() on it. You should be able to call json.Door0Vld immediately (or maybe json[0].Door0Vld based on how exactly your JSON is structured).

4 Likes

Thank you njschwartz & ahndee! There are actually 2 issues and each one is mentioned in your replies. I forced extra " when encoding json on server in desperation to make json to be decoded on ST side. The second problem is msg.json is already parsed like ahndee mentioned.

It fixed my problem by fixing up the encoding side and use msg.json straight up. Thanks!