title: Convert a directory structure in the filesystem to JSON with Node.js
body: <p>I have a file structure like this:</p>
  
  <pre><code>root
  |_ fruits
  |___ apple
  |______images
  |________ apple001.jpg
  |________ apple002.jpg
  |_ animals
  |___ cat
  |______images
  |________ cat001.jpg
  |________ cat002.jpg
  </code></pre>
  
  <p>I would like to, using Javascript and Node.js, listen to this root directory and all sub directories and create a JSON which mirrors this directory structure, each node contains type, name, path, and children:</p>
  
  <pre><code>data = [
    {
      type: "folder",
      name: "animals",
      path: "/animals",
      children: [
        {
          type: "folder",
          name: "cat",
          path: "/animals/cat",
          children: [
            {
              type: "folder",
              name: "images",
              path: "/animals/cat/images",
              children: [
                {
                  type: "file",
                  name: "cat001.jpg",
                  path: "/animals/cat/images/cat001.jpg"
                }, {
                  type: "file",
                  name: "cat001.jpg",
                  path: "/animals/cat/images/cat002.jpg"
                }
              ]
            }
          ]
        }
      ]
    }
  ];
  </code></pre>
  
  <p>Here's a coffeescript JSON:</p>
  
  <pre><code>data = 
  [
    type: "folder"
    name: "animals"
    path: "/animals"
    children  :
      [
        type: "folder"
        name: "cat"
        path: "/animals/cat"
        children:
          [
            type: "folder"
            name: "images"
            path: "/animals/cat/images"
            children: 
              [
                type: "file"
                name: "cat001.jpg"
                path: "/animals/cat/images/cat001.jpg"
              , 
                type: "file"
                name: "cat001.jpg"
                path: "/animals/cat/images/cat002.jpg"
              ]
          ]
      ]
  ]
  </code></pre>
  
  <p>how to get this json data format in django views?(python)</p>