title: Java: replace nested json object using jackson streaming api
body: <p>I have a huge JSON file with lots of nested objects inside. I need to find particular node in that JSON (here I don't have any issues) and replace nested object with java POJO. Important thing is that I'm not able to read whole JSON file to java object as I have memory and performance limitations. So for this purpose I need to use jackson stream API and some how replace required part of JSON right during streaming. Could please anyone advice on how to do so?</p>
  <p>Below you can find JSON example (it is super simplified just for simplicity of example) and part of code.</p>
  <p><strong>Initial JSON:</strong></p>
  <pre><code>{
      &quot;mainObject&quot;: {
          &quot;someChildObject&quot;: {
              &quot;lol&quot;: 123,
              &quot;kek&quot;: &quot;cheburek&quot;
          },
          &quot;exactlyThisPartWillBeChanged&quot;: {
              &quot;16&quot;: {
                  &quot;number&quot;: 16,
                  &quot;name&quot;: &quot;blah blah&quot;
              },
              &quot;12&quot;: {
                  &quot;number&quot;: 12,
                  &quot;name&quot;: &quot;blah blah&quot;
              },
              &quot;11&quot;: {
                  &quot;number&quot;: 11,
                  &quot;name&quot;: &quot;blah blah&quot;
              }
          },
          &quot;someOtherChildObject&quot;: {
              &quot;ololol&quot;: 666,
              &quot;someOtherProperty&quot;: &quot;smachnyj&quot;
          }
      }
  }
  </code></pre>
  <p><strong>Java code:</strong></p>
  <pre><code>import com.fasterxml.jackson.core.JsonParser;
  import com.fasterxml.jackson.core.JsonToken;
  import com.fasterxml.jackson.databind.DeserializationFeature;
  import com.fasterxml.jackson.databind.ObjectMapper;
  import lombok.Data;
  import org.junit.Test;
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.nio.file.Paths;
  import java.util.HashMap;
  
  public class JsonTest {
  
      @Test
      public void test() throws IOException {
          File initialFile = new File(&quot;file.json&quot;); // original file with JSON
          InputStream srcInputStream = new FileInputStream(initialFile);
  
          ObjectMapper mapper = new ObjectMapper();
          mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
  
          try (JsonParser jsonParser = mapper.getFactory().createParser(srcInputStream)) {
              TestClassMap testClassMap;
  
              // Iterate over the tokens until find token with particular name
              while (jsonParser.nextToken() != JsonToken.VALUE_TRUE) {
                  jsonParser.nextToken();
                  String currentName = jsonParser.getCurrentName();
  
                  if (&quot;exactlyThisPartWillBeChanged&quot;.equalsIgnoreCase(currentName)) { // exactlyThisPartWillBeChanged  parameter in JSON which I want to replace
                      jsonParser.nextToken();
                      testClassMap = mapper.readValue(jsonParser, TestClassMap.class);
  
                      testClassMap.get(&quot;16&quot;).setName(&quot;test&quot;); // updating some random value in the object
  
                      // here should be some logic to swap whole &quot;exactlyThisPartWillBeChanged&quot; node in json with updated and serialized testClassMap object
  
                      mapper.writeValue(Paths.get(&quot;file2.json&quot;).toFile(), testClassMap); // as a result here should be file with original JSON but with replaced part
  
                      jsonParser.close();
                  }
              }
          }
      }
  }
  
  @Data
  class TestClass {
      Integer number;
      String name;
  }
  
  class TestClassMap extends HashMap&lt;String, TestClass&gt; {
  }
  </code></pre>