title: Reading streaming JSON in Groovy
body: <p>We're trying to read streaming JSON data in Groovy (using JsonSlurper) but we only manage to do it by repeatedly parsing the source URL. What we would like to do is for it to be a push mechanism so that any time there is new content we get a callback. Is that possible? (if you have a solution in Java, that's fine too)</p>
  
  <pre><code>package json
  
  import groovy.json.JsonParserType
  import groovy.json.JsonSlurper
  
  class JsonParser {
      private static final String JSON_URL = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20csv%20where%20url%3D'http%3A%2F%2Fdownload.finance.yahoo.com%2Fd%2Fquotes.csv%3Fs%3DHM-B.ST%26f%3Dsl1d1t1c1ohgv%26e%3D.csv'%20and%20columns%3D'symbol%2Cprice%2Cdate%2Ctime%2Cchange%2Ccol1%2Chigh%2Clow%2Ccol2'&amp;format=json&amp;env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"
  
      static main(args) {
          def url = new URL(JSON_URL)
          def urlStream = null
          try {
              urlStream = url.openStream()
              def jsonSlurper = new JsonSlurper().setType(JsonParserType.INDEX_OVERLAY)
              def jsonObj = jsonSlurper.parse(url)
              10.times {
                  println jsonObj.query.created.toString()
                  //println jsonObj.toString()
                  jsonObj = jsonSlurper.parse(url)
                  sleep(1000)
              }
          } finally {
              if (urlStream != null) urlStream.close()
          }
      }
  
  }
  </code></pre>