title: C# endpoint giving back a nested list of directories and files as JSON
body: <p>My goal is to implement an endpoint which gives back a nested list of all files and directories starting from a certain root directory, e.g. <code>C:\Temp</code>. I have written the following code:</p>
  
  <pre class="lang-cs prettyprint-override"><code>namespace API.Controllers
  {
      public class UploadController : BaseController
      {
          [Route("api/Uploaded", Order = -1)]
          [ResponseType(typeof(IEnumerable&lt;string[]&gt;))] // &lt;- this has to be adjusted, I guess.
          public IHttpActionResult AutoUpload()
          {
              string[] entries = Directory.GetFileSystemEntries("C:\Temp", "*", SearchOption.AllDirectories);
              // &lt;-- Here should come some conversion to a nested JSON.
              return Ok(entries); 
          }
      }
  }
  </code></pre>
  
  <p>When I am querying the endpoint, e.g. with <code>curl -X POST --header 'Accept: application/json' 'http://localhost:63291/api/Uploaded'</code> the response is something</p>
  
  <pre><code>[
   "C:\\\\Temp\\file1",
   "C:\\\\Temp\\dir1\\file2",
   "C:\\\\Temp\\dir1\\file3"
  ]
  </code></pre>
  
  <p>What I would rather like to have is something like the following</p>
  
  <pre><code>[
      { "~":
          [ "file1" ]
      },
      {
          "~/dir1":
                  [
                       "file2" ,
                       "file3" 
                 ]
      }
  ]
  </code></pre>
  
  <p>I am sure it cannot be so complicated to convert my list into a nested JSON, but I somehow do not manage to do it. I fear, I am missing the appropriate search terms. Please help!</p>