26

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())
4
  • 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 stackoverflow.com/questions/15389329/… Commented Oct 22, 2013 at 16:05
  • null pointer exception at line beginning "game = ..." Commented Oct 22, 2013 at 16:16
  • Issue was resolved using an alternative code Commented Oct 23, 2013 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 Commented Oct 23, 2013 at 19:53

4 Answers 4

67

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 }
Sign up to request clarification or add additional context in comments.

1 Comment

With this solution, I kept getting a java.io.FileNotFoundException: and realized I needed to change new File to readFile as stated here: stackoverflow.com/a/52752719/4535181 and here stackoverflow.com/a/38679858/4535181 Additionally, I had to remove the call to the text property of inputFile
7

try:

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

2 Comments

I use your code exacly, but it shows "nullPointerException" error at JsonSlurper line...
Can you add the actual stack trace to the question?
1

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 }

1 Comment

public Object parse(File file) is available in JsonSlurper since version 2.2.0 - see groovy docs
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()
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.