title: Reading JSON object from txt file in Groovy
body: <p>I am trying to collect a JSON from a txt file. But my below code seems to keep giving me "nullPointerException". </p>
  
  <pre><code>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));
      }
  } 
  </code></pre>
  
  <p>SOLUTION FOUND<br>
  Reading a json txt file:</p>
  
  <pre><code>File f = new File(tempDir+File.separator+'jsonObject.txt')
  def slurper = new JsonSlurper()
  def jsonText = f.getText()
  json = slurper.parseText( jsonText )
  </code></pre>
  
  <p>Writing json to a file:</p>
  
  <pre><code>File g = new File(tempDir+File.separator+'jsonObject.txt')
              g.createNewFile()
              def json = new JsonBuilder()
              json {
                  "result" result
                  }       
              g.setText(json.toString())
  </code></pre>