title: Streaming large JSON from input stream efficiently in Java
body: <p>In order to save memory and avoid an OOM error, I want to stream a large JSON from an input stream and extract the desired things from it. More exactly, I want to extract and save some strings from that JSON:</p>
  <ol>
  <li>files.content.fileContent.subList.text = &quot;some text in file&quot;</li>
  <li>files.content.fileContent.subList.text = &quot;some text in file2&quot;</li>
  </ol>
  <p>and save them into a String variable:</p>
  <pre><code>String result = &quot;some text in file \r\nsome text in file2&quot;
  </code></pre>
  <p>I tried to parse the JSON using Jackson:</p>
  <pre><code>        JsonFactory jsonFactory = new JsonFactory();
  
          StringBuilder result = new StringBuilder();
          try (JsonParser jsonParser = jsonFactory.createParser(jsonAsInputStream)) {
              String fieldName;
              while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                  jsonParser.nextToken();
                  fieldName = jsonParser.getCurrentName();
                  if (&quot;files&quot;.equals(fieldName)) {
  
                      while (true) {
                          jsonParser.nextToken();
                          fieldName = jsonParser.getCurrentName();
                          if (&quot;content&quot;.equals(fieldName)) {
                              jsonParser.nextToken();
                              fieldName = jsonParser.getCurrentName();
                              while (true) {
                                  if (&quot;text&quot;.equals(fieldName)) {
                                      result.append(jsonParser.getText());
                                  }
                              }
                          }
                      }
                  }
              }
              LOGGER.info(&quot;result: {}&quot;, result);
          } catch (JsonParseException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
  </code></pre>
  <p>The above is not working at all, that solution gets complicated. Is there any simple way to parse the JSON inputStream and extract some text from it?</p>
  <p>Below is the JSON attached:</p>
  <pre><code>{
  &quot;id&quot;: &quot;1&quot;,
  &quot;name&quot;: &quot;TestFile.xlsx&quot;,
  &quot;files&quot;: [
      {
          &quot;id&quot;: &quot;1&quot;,
          &quot;fileName&quot;: &quot;TestFile.xlsx&quot;,
          &quot;types&quot;: {
              &quot;fileId&quot;: &quot;1&quot;,
              &quot;context&quot;: [
                  {
                      &quot;id&quot;: 1,
                      &quot;contextId&quot;: &quot;xyz&quot;,
                      &quot;metadata&quot;: {
                          &quot;abc&quot;: &quot;1&quot;
                      }
                  },
                  {
                      &quot;id&quot;: 2,
                      &quot;contextId&quot;: &quot;abc&quot;
                  }
              ],
              &quot;fileSettings&quot;: [
                  {
                      &quot;id&quot;: 1,
                      &quot;settingId&quot;: 1
                  },
                  {
                      &quot;id&quot;: 2,
                      &quot;settingId&quot;: 2
                  }
                  
              ],
              &quot;fileAttachments&quot;: [
                  {
                      &quot;id&quot;: 1,
                      &quot;canDelete&quot;: true,
                      &quot;canAttach&quot;: []
                  }
              ],
              &quot;replacements&quot;: [
                  {
                      &quot;id&quot;: 1,
                      &quot;replacementText&quot;: &quot;xcv&quot;
                  }
              ]
          },
          &quot;content&quot;: [
              {
                  &quot;id&quot;: &quot;1&quot;,
                  &quot;contextList&quot;: [
                      1,
                      2,
                      3
                  ],
                  &quot;fileContent&quot;: {
                      &quot;contentType&quot;: &quot;text&quot;,
                      &quot;subList&quot;: [
                          {
                              &quot;id&quot;: &quot;1&quot;,
                              &quot;subList&quot;: [
                                  {
                                      &quot;id&quot;: &quot;1&quot;,
                                      &quot;text&quot;: &quot;some text in file&quot;,
                                      &quot;type&quot;: &quot;text&quot;
                                  }
                              ]
                          }
                      ]
                  },
                  &quot;externalContent&quot;: {
                      &quot;id&quot;: &quot;1&quot;,
                      &quot;children&quot;: [
                          {
                              &quot;id&quot;: &quot;1&quot;,
                              &quot;contentType&quot;: &quot;text corrupted&quot;,
                              &quot;children&quot;: []
                          }
                      ]
                  }
              },
              {
                  &quot;id&quot;: &quot;2&quot;,
                  &quot;contextList&quot;: [
                      1,
                      2
                  ],
                  &quot;fileContent&quot;: {
                      &quot;contentType&quot;: &quot;text&quot;,
                      &quot;subList&quot;: [
                          {
                              &quot;id&quot;: &quot;2&quot;,
                              &quot;subList&quot;: [
                                  {
                                      &quot;id&quot;: &quot;1&quot;,
                                      &quot;text&quot;: &quot;some text in file2&quot;,
                                      &quot;type&quot;: &quot;text&quot;
                                  }
                              ]
                          }
                      ]
                  },
                  &quot;externalContent&quot;: {
                      &quot;id&quot;: &quot;2&quot;,
                      &quot;children&quot;: [
                          {
                              &quot;id&quot;: &quot;2&quot;,
                              &quot;contentType&quot;: &quot;text corrupted2&quot;,
                              &quot;children&quot;: []
                          }
                      ]
                  }
              }
          ]
      }
  ]
  </code></pre>
  <p>}</p>