24

I am trying to collect a JSON from a txt file. But my below code seems to keep giving me "nullPointerException".

File f = new File(tempDir+File.separator+'jsonObject.txt')
if (f){
    log.error " file exists $f"
    FileReader f2 = new FileReader(f);
    log.error " file data- $f2"
    if (f2 == null) {
        //do something
    } else {
        JsonSlurper jsonParser = new JsonSlurper();
        game = jsonParser.parse(new FileReader(f));
    }
} 

SOLUTION FOUND
Reading a json txt file:

File f = new File(tempDir+File.separator+'jsonObject.txt')
def slurper = new JsonSlurper()
def jsonText = f.getText()
json = slurper.parseText( jsonText )

Writing json to a file:

File g = new File(tempDir+File.separator+'jsonObject.txt')
            g.createNewFile()
            def json = new JsonBuilder()
            json {
                "result" result
                }       
            g.setText(json.toString())
Sanket Deshpande
  • 293
  • 2
  • 3
  • 10
  • where is the null pointer being thrown? You may also want to look at this question I asked a while back around reading in text files http://stackoverflow.com/questions/15389329/where-to-put-a-text-file-in-grails-and-how-to-get-the-path – andy mccullough Oct 22 '13 at 16:05
  • null pointer exception at line beginning "game = ..." – Sanket Deshpande Oct 22 '13 at 16:16
  • Issue was resolved using an alternative code – Sanket Deshpande Oct 23 '13 at 19:27
  • 2
    For anyone following this, you don't need to do what the _solution_ here says, you can read from a Reader, and writing can be done via `new File( f ).text = new JsonBuilder( [ result: result ] )` rather than the 7 lines above – tim_yates Oct 23 '13 at 19:53

4 Answers4

63

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 }
olyv
  • 3,699
  • 5
  • 37
  • 67
  • With this solution, I kept getting a _java.io.FileNotFoundException:_ and realized I needed to change `new File` to `readFile` as stated here: https://stackoverflow.com/a/52752719/4535181 and here https://stackoverflow.com/a/38679858/4535181 Additionally, I had to remove the call to the **text** property of `inputFile` – dreamwork801 Oct 02 '20 at 11:49
7

try:

File f = new File( tempDir, 'jsonObject.txt' )
if( f.exists() ) {
    def game = f.withReader { r ->
        new JsonSlurper().parse( r )
    }
    println game
}
tim_yates
  • 167,322
  • 27
  • 342
  • 338
1

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()
    }
Rahul Mahadik
  • 11,668
  • 6
  • 41
  • 54
0

parseFile can take a file as an input:

import groovy.json.JsonSlurper

def inputFile = new File("/your/path/my.json")
def InputJSON = new JsonSlurper().parseFile(inputFile, 'UTF-8')
InputJSON.each{ println it }
  • 2
    `public Object parse(File file)` is available in JsonSlurper since version 2.2.0 - see [groovy docs](http://docs.groovy-lang.org/2.2.0/html/gapi/groovy/json/JsonSlurper.html#parse(java.io.File)) – user1053510 Jul 23 '18 at 10:02