JsonSlurper returns No signature of method: groovy.json.JsonSlurper.parseText() is applicable for argument types: (java.util.ArrayList)

11,213

According to the documentation, the HTTPBuilder could be parsing your JSON for you. If your JSON response has its root as a JSON array, then that explains the ArrayList object in your reader variable.

Regarding how this explains the exception being thrown. The reader parameter of the Closure is an ArrayList of parsed JSON, not a String of unparsed JSON. Thus, the code fails on new JsonSlurper().parseText(reader) because reader is not text and the JsonSlurper does not have a method defined for how to parse an ArrayList as JSON.

Share:
11,213
Tinolover
Author by

Tinolover

Fullstack engineer at Granular.

Updated on June 04, 2022

Comments

  • Tinolover
    Tinolover almost 2 years

    I'm trying to parse JSON file with JsonSlurper.parseText but keep getting similar problems.

    def jsonParse = null
    def http = new HTTPBuilder(url)
    http.auth.basic(username, password)
    http.request(Method.GET) {
        response.success = { resp, reader ->;
          jsonParse = new JsonSlurper().parseText(reader)
        }
    }
    

    Whenever I run my application the error message says

    No signature of method: groovy.json.JsonSlurper.parseText() is applicable for argument types: (java.util.ArrayList)

    I understand that JsonSlurper.parseText() is asking for a java.util.ArrayList type as an input. So I tried the following to figure out the type of the input using this code.

    def jsonParse = null
    def http = new HTTPBuilder(url)
    http.auth.basic(username, password)
    http.request(Method.GET) {
        response.success = { resp, reader ->;
          jsonParse = reader
        }
    }
    render jsonParse.getClass()
    

    This prints out the following:

    class java.util.ArrayList
    

    I don't understand why I'm getting this error when I am feeding the input with correct datatype.

    Any suggestions?

  • Tinolover
    Tinolover almost 9 years
    God... I swear I would've never thought of HTTPBuilder to parse JSON for me... Thank you so much!!
  • Cartitza
    Cartitza almost 5 years
    This solves OP's problem but doesn't answer OP's question. This question keeps popping in google results when searching for this question's answer, but accepted answer for this question is offering a work-around, not an aswer to the actual problem: JsonSlurper returns No signature of method: groovy.json.JsonSlurper.parseText() is applicable for argument types: (java.util.ArrayList)