title: how to parse a huge JSON file without loading it in memory
body: <p>I have a large JSON file (2.5MB) containing about 80000 lines.</p>
  
  <p>It looks like this:</p>
  
  <pre><code>{
    "a": 123,
    "b": 0.26,
    "c": [HUGE irrelevant object],
    "d": 32
  }
  </code></pre>
  
  <p>I only want the integer values stored for keys <code>a</code>, <code>b</code> and <code>d</code> and ignore the rest of the JSON (i.e. ignore whatever is there in the <code>c</code> value).</p>
  
  <p>I cannot modify the original JSON as it is created by a 3rd party service, which I download from its server.</p>
  
  <p>How do I do this without loading the entire file in memory?</p>
  
  <p>I tried using <a href="https://github.com/google/gson" rel="noreferrer">gson</a> library and created the bean like this:</p>
  
  <pre><code>public class MyJsonBean {
    @SerializedName("a")
    @Expose
    public Integer a;
  
    @SerializedName("b")
    @Expose
    public Double b;
  
    @SerializedName("d")
    @Expose
    public Integer d;
  }
  </code></pre>
  
  <p>but even then in order to deserialize it using Gson, I need to download + read the whole file in memory first and the pass it as a string to Gson?</p>
  
  <pre><code>File myFile = new File(&lt;FILENAME&gt;);
  myFile.createNewFile();
  
  URL url = new URL(&lt;URL&gt;);
  OutputStream out = new BufferedOutputStream(new FileOutputStream(myFile));
  URLConnection conn = url.openConnection();
  
  HttpURLConnection httpConn = (HttpURLConnection) conn;
  
  InputStream in = conn.getInputStream();
  byte[] buffer = new byte[1024];
  
  int numRead;
  while ((numRead = in.read(buffer)) != -1) {
    out.write(buffer, 0, numRead);
  }
  
  FileInputStream fis = new FileInputStream(myFile);
  byte[] data = new byte[(int) myFile.length()];
  fis.read(data);
  String str = new String(data, "UTF-8");
  
  Gson gson = new Gson();
  MyJsonBean response = gson.fromJson(str, MyJsonBean.class);
  
  System.out.println("a: " + response.a + "" + response.b + "" + response.d);
  </code></pre>
  
  <p>Is there any way to avoid loading the whole file and just get the relevant values that I need?</p>