Reading JSON object from txt file in Groovy

try:

File f = new File( tempDir, 'jsonObject.txt' )
if( f.exists() ) {
    def game = f.withReader { r ->
        new JsonSlurper().parse( r )
    }
    println game
}

Try simple and optimized solution:

import groovy.json.JsonSlurper

try {
File inputFile = new File("your_file_path")
def slurper = new JsonSlurper()
def data = slurper.parse(inputFile)
} catch (Exception e) {
      e.printStackTrace()
    }

Please, try this:

import groovy.json.JsonSlurper

def inputFile = new File("D:\\yourPath\\json.txt")
def InputJSON = new JsonSlurper().parseText(inputFile.text)
InputJSON.each{ println it }

Tags:

Grails

Groovy