Paul
Paul

Reputation: 1355

groovy.json.JsonSlurper parse JSON

I'm trying to parse a JSON-File in my gradle task.

CODE:

def jsonFile = "../files/json/myJSON.json"
def list = new JsonSlurper().parseText(jsonFile)

JSON - FILE

{
   "prepare": {
       "installed": [],
       "uninstalled": []
   },
   "config": {
       "files": []
   }
}

But the code gives me the following exception:

Lexing failed on line: 1, column: 1, while reading '.', no possible valid JSON value or punctuation could be recognized.

And I don't understand why, I also validated my JSON-File on http://jsonlint.com/ and it says that it is a valid JSON!

Upvotes: 1

Views: 6370

Answers (1)

Peter Niederwieser
Peter Niederwieser

Reputation: 123986

Above code is trying to parse the string ../files/json/myJSON.json as JSON. Instead use:

def jsonFile = new File("../files/json/myJSON.json")
def map = new JsonSlurper().parse(jsonFile)

Upvotes: 1

Related Questions