Search code examples
jsonparsinggrailsjsonslurper

JSONException error Grails


Paring JSON file, using JsonSlurper

My code is like below

String url = 'https://urlThatIWantToGoto'
String jsonFile = new JsonSlurper.parseText(new URL(url).text))
JSONArray jsonParse = new JSONArray(jsonFile)

Whenever I run this code, I get a error printing as following

Caught: org.codehaus.groovy.grails.web.json.JSONException: Expected a ',' or '}' at character 982 of "MyJSONFile"

Funny thing is that it works with one of the example JSON url I have, and failes for the other two. (I've checked and confirmed that all three of the urls contain valid JSON files)

Can anyone tell me what is wrong with my code?


Solution

  • I got this working. Here is the code:

        def url = "https://gist.githubusercontent.com/lee910.../test2"
        def jsonResponse = new JsonSlurper().parseText(new URL(url).text)
        println "== ${jsonResponse.size()}: ${jsonResponse[0]}"
    

    So, notice that jsonResponse is already an array so no need for the JSONArray. Printing gives:

    == 30: [course:[guid:2f093ff2-913b-4081-a800-238200f1c559], cr...
    

    One last thing. Make sure your slurper belongs to import groovy.json.JsonSlurper just in case.