title: How to read an unformatted json stream and write to a formatted json file with minimal memory usage?
body: <p>I'm calling an API that return a response of type application/json. The response can be very tiny but it can be very huge, 500mb to 700mb. I would like to format the content (indentation, new lines, etc) and write the response to a json file with the help of <code>System.Text.Json</code> so the file can be read easely by an humain.</p>
  <p>This code write the response stream directly to a file very efficiently on memory and speed but it doesn't format the json content. Since that the stream is directly written to the file, it doesn't take memory at all.</p>
  <pre><code>var response = await new HttpClient().GetAsync(&quot;an url&quot;, HttpCompletionOption.ResponseHeadersRead);
  var responseStream = await response.Content.ReadAsStreamAsync();
  
  using (var fileStream = File.Open(filePath, FileMode.Create))
  {
      await responseStream.CopyToAsync(fileStream);
  }
  </code></pre>
  <p>I tried this code to add the formatting but it doesn't seems right since that it use over 1gb of memory.</p>
  <pre><code>var response = await new HttpClient().GetAsync(&quot;an url&quot;, HttpCompletionOption.ResponseHeadersRead);
  var responseStream = await response.Content.ReadAsStreamAsync();
  var jsonDocument = System.Text.Json.JsonDocument.Parse(responseStream);
  var jsonWriterOptions = new System.Text.Json.JsonWriterOptions()
  {
      Indented = true
  };
  
  using (var fileStream = File.Open(filePath, FileMode.Create))
  using (var jsonTextWriter = new System.Text.Json.Utf8JsonWriter(fileStream, jsonWriterOptions))
  {
      jsonDocument.WriteTo(jsonTextWriter);
  }
  </code></pre>
  <p>Is there a more optimized way, that use less memory, to deal with huge content?</p>