Sweet
Sweet

Reputation: 187

Push JSON data to a file using Java

I am calling a REST API Get method using java and the JSON data needs to be stored into a file. Here is my JSON data:

{
 "took" : 25,
 "timed_out" : false,
 "_shards" : {
 "total" : 5,
 "successful" : 5,
 "failed" : 0
},
 "hits" : {
  "total" : 820,
  "max_score" : 1.0,
  "hits" : [ {
   "_index" : "test",
   "_type" : "status",
   "_id" : "abc-2345-dadasasdasd214124",
   "_score" : 1.0,
   "_source":
    {"A":1,"B":12,"C":122,"D":89,"E":120,"F":100,"G":2}
  }, {
   "_index" : "test",
   "_type" : "status",
   "_id" : "abc-2345-dadasasdasd214124",
   "_score" : 1.0,
   "_source":
    {"A":54,"B":171,"C":102,"D":0,"E":120,"F":11,"G":20}
 } , {
  "_index" : "test",
  "_type" : "status",
  "_id" : "abc-2345-dadasasdasd214124",
  "_score" : 1.0,
  "_source":
  {"A":94,"B":8,"C":155,"D":32,"E":90,"F":11,"G":0}
} ]
}
}

Here is my code:

public class testcode {

public static void main(String[] args) throws ClientProtocolException, IOException {
      HttpClient client = new DefaultHttpClient();

      try {
          HttpGet httpGetRequest = new HttpGet("http://localhost:8080/test_search?pretty=1");
          HttpResponse httpResponse = client.execute(httpGetRequest);

          HttpEntity entity = httpResponse.getEntity();

          byte[] buffer = new byte[1024];
          if (entity != null) {
            InputStream inputStream = entity.getContent();
            try {
              int bytesRead = 0;
              BufferedInputStream bis = new BufferedInputStream(inputStream);
              FileWriter file = new FileWriter("/path/test.json");

              while ((bytesRead = bis.read(buffer)) != -1) {
                String chunk = new String(buffer, 0, bytesRead);
                System.out.println(chunk);
                file.write(chunk);


              }
            } catch (Exception e) {
              e.printStackTrace();
            } finally {
              try { inputStream.close(); } catch (Exception ignore) {}
            }
          }
        } catch (Exception e) {
          e.printStackTrace();
        } finally {
          client.getConnectionManager().shutdown();
        }

}

After running this code, I am seeing the JSON data in my console but the entire data is not getting saved into the test.json file. The first two arrays are getting saved completely but only a part of the third array is getting saved into the test.json.

Data in test.json file:

{
 "took" : 25,
  "timed_out" : false,
  "_shards" : {
  "total" : 5,
  "successful" : 5,
  "failed" : 0
},
 "hits" : {
  "total" : 820,
  "max_score" : 1.0,
  "hits" : [ {
   "_index" : "test",
   "_type" : "status",
   "_id" : "abc-2345-dadasasdasd214124",
   "_score" : 1.0,
   "_source":
     {"A":1,"B":12,"C":122,"D":89,"E":120,"F":100,"G":2}
   }, {
   "_index" : "test",
   "_type" : "status",
   "_id" : "abc-2345-dadasasdasd214124",
   "_score" : 1.0,
   "_source":
     {"A":54,"B":171,"C":102,"D":0,"E":120,"F":11,"G":20}
   } , {
    "_index" : "test",
    "_type" : "status",
    "_id" : "abc-2345-dadasasdasd214124",
    "_score" : 1.0,
    "_source":
     {"A":94,"B":8,"C":155,"D":32,"E

NOTE: THe json data given above is a sample data. The real json file contains lot of data. Please advice. Thanks

Upvotes: 1

Views: 484

Answers (1)

tschaible
tschaible

Reputation: 7695

Try also ensuring the FileWriter is closed.

} finally {
  try { file.close(); inputStream.close(); } catch (Exception ignore) {}
}

Your program may be exiting while some of the data is still buffered and has not been written to the file.

Upvotes: 1

Related Questions