title: D3.js nesting and rollup at same time
body: <p>I am looking to convert a csv file into a particular JSON format for a D3.js visualization.  The basic idea is to simultaneously nest and rollup at each level.  In other words, parents at every level in the structure would contain the sum of their children. </p>
  
  <p>An example csv file is the following:</p>
  
  <pre><code>Country,State,City,Population,
  "USA","California","Los Angeles",18500000,
  "USA","California","San Diego",1356000,
  "USA","California","San Francisco",837442,
  "USA","Texas","Austin",885400,
  "USA","Texas","Dallas",1258000,
  "USA","Texas","Houston",2196000
  </code></pre>
  
  <p>The JSON format that I am hoping to create is as follows:</p>
  
  <pre><code>[
    {
      key: "USA",
      Population: 25032842,
      values: [
        {
          key: "California",
          Population: 20693442,
          values: [
            {
              key: "Los Angeles",
              Population: 18500000
            },
            {
              key: "San Diego",
              Population: 1356000
            },
            {
              key: "San Francisco",
              Population: 837442
            }
          ]
        },
        {
          key: "Texas",
          Population: 4339400,
          values: [
            {
              key: "Austin",
              Population: 885400
            },
            {
              key: "Dallas",
              Population: 1258000
            },
            {
              key: "Houston",
              Population: 2196000
            }
          ]
        }
      ]
    }
  ]
  </code></pre>