title: Prettify a JSON string in C++ from a .txt file
body: <p>I'm currently working in C++, getting an HTTP response from a request that I write into a .txt file using ostream. This happens asynchronously and I don't want to change this.</p>
  
  <p>Once the data is done being written, I want to read from the file</p>
  
  <p><code>{"data":{"request":[{"type":"City","query":"London, United Kingdom"}],"weather":[{"date":"2013-04-21","astronomy".....</code></p>
  
  <p>~somehow~ prettify the string using either an outside library like nlohmann/json or other(?) and then</p>
  
  <p>a)print it to the console and 
  b) save it in a different file (pretty.json)</p>
  
  <p>I am having trouble understanding which method to use from:
  <a href="https://github.com/nlohmann/json" rel="noreferrer">https://github.com/nlohmann/json</a></p>
  
  <p>Any ideas how to approach this?</p>
  
  <p>I was thinking getting the file line by line until I hit EOF into a sort of "buffer" and then running _json on that and saving the solution which can be displayed on the console...</p>
  
  <p>My code so far</p>
  
  <pre><code>#include &lt;cpprest/http_client.h&gt;
  #include &lt;cpprest/filestream.h&gt;
  #include &lt;iostream&gt;
  #include &lt;sstream&gt;
  #include "json.hpp"
  
  
  
  using namespace utility;                // string conversion
  using namespace web;                    // URI 
  using namespace web::http;              // HTTP commands
  using namespace web::http::client;      // HTTP Client features
  using namespace concurrency::streams;   // Asynch streams, like Node
  
  using json = nlohmann::json;
  
  int main()
  {
  auto fileStream = std::make_shared&lt;ostream&gt;();
  
  // Open stream to output file.
  pplx::task&lt;void&gt; requestTask = fstream::open_ostream(U("results.txt"))
  
  .then([=](ostream outFile)
  
  {
      *fileStream = outFile;
  
      http_client client //gets the info
      return client.request(methods::GET, stringBuilder.to_string());
  })
  
      .then([=](http_response response)       // set up response handler
  {
      printf("Received response status code:%u\n", response.status_code()); 
  
      return response.body().read_to_end(fileStream-&gt;streambuf());
  
  })      
  
      .then([=](size_t)       // close file stream
  {
      return fileStream-&gt;close();
  })
  
      .then([=]()
  {
      nlohmann::json j;
      std::ifstream i;
      i.open("results.txt"); // ?? &lt;&lt;&lt; === this is where my question is
  });
  
  // Wait for all the outstanding I/O to complete, handle exceptions
  try
  {
      requestTask.wait();
  }
  catch (const std::exception &amp;e) 
  {
      printf("Error exception:%s\n", e.what());
    }
  
  
    return 0;
  }
  </code></pre>
  
  <hr>
  
  <p>SOLUTION: </p>
  
  <pre><code>.then([=]()
      {
  
      // read a JSON file
      std::ifstream readFromFile("results.txt");
      if (readFromFile.is_open()) {
  
      nlohmann::json j;
      readFromFile &gt;&gt; j;
  
      // write prettified JSON to another file
      std::ofstream writeToFile("pretty.json");
      writeToFile &lt;&lt; std::setw(4) &lt;&lt; j &lt;&lt; std::endl;
  
      readFromFile.close();
      writeToFile.close();
      }
      else {
          std::cout &lt;&lt; "unable to open file";
      }
  
   });
  </code></pre>