0

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?

Tinolover
  • 166
  • 1
  • 5
  • 19
  • 2
    You've got to show the json string you are receiving. Add` println JsonOutput.prettyPrint(jsonFile)` and let's see what you have. The error clearly shows problems with the json string. – mohsenmadi Jun 02 '15 at 15:56
  • @mohsenmadi https://gist.githubusercontent.com/lee910705/116ddc48f9d1c08a81e4/raw/68770e9d32886c6d1257395d86391de3f04e232c/test2 https://gist.githubusercontent.com/lee910705/69f29ed04dc919eb1606/raw/7598dd0da7b3da2c0438733c2a1c4d1c4edecfea/test3 These are two JSON files that raises this error. I don't see anything wrong in these two files though... – Tinolover Jun 02 '15 at 17:24

1 Answers1

1

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.

mohsenmadi
  • 2,277
  • 1
  • 23
  • 34