title: How can we iterate over a JSON file of size around 2 gb using Java
body: <p>I am reading 3 column values from the database (around 50 000 records) and then trying to search this value in Json file. Json file is containing 2 million Json objects.
  I have tried the below approaches.</p>
  
  <p><strong>Approach 1.</strong></p>
  
  <pre><code>JSONArray json = readJson(Constants.jsonFilePath);
  
  private JSONArray readJson(String jsonFilePath) {
      String content = null;
      File file = new File(Constants.jsonFilePath);
      try {
          content = FileUtils.readFileToString(file, "utf-8");
      } catch (IOException e) {
          e.printStackTrace();
      }
      return new JSONArray(content);
  
  }
  </code></pre>
  
  <p>And then linearly searching the required field value    </p>
  
  <p>I tested the above code against the file of size 150 MB and it worked very well. But when I was testing it for the file of size 2 gb then I am getting OutOfHeapMemory error.</p>
  
  <p><strong>Approach 2:</strong></p>
  
  <p>Then I tried to read 100 000 Json objects at a time from the file and then check the required field value but the process is tremendously slow. </p>
  
  <p>I am using org.json library.
  Is there any better way to solve the above problem?</p>