title: csv to nested JSON?
body: <p>I’m trying to convert a flat structured CSV into a nested JSON structure.  The CSV is generated from SQL which creates multiple rows for each primary id. The CSV is structured as follows:</p>
  
  <pre class="lang-none prettyprint-override"><code>PrimaryId,FirstName,LastName,City,CarName,DogName
  100,John,Smith,NewYork,Toyota,Spike
  100,John,Smith,NewYork,BMW,Spike
  100,John,Smith,NewYork,Toyota,Rusty
  100,John,Smith,NewYork,BMW,Rusty
  101,Ben,Swan,Sydney,Volkswagen,Buddy
  101,Ben,Swan,Sydney,Ford,Buddy
  101,Ben,Swan,Sydney,Audi,Buddy
  101,Ben,Swan,Sydney,Volkswagen,Max
  101,Ben,Swan,Sydney,Ford,Max
  101,Ben,Swan,Sydney,Audi,Max
  102,Julia,Brown,London,Mini,Lucy
  </code></pre>
  
  <p>The desired JSON output is:</p>
  
  <pre><code>{
      "data": [
          {
              "City": "NewYork", 
              "FirstName": "John", 
              "PrimaryId": 100, 
              "LastName": "Smith", 
              "CarName": [
                  "Toyota", 
                  "BMW"
              ], 
              "DogName": [
                  "Spike", 
                  "Rusty"
              ]
          }, 
          {
              "City": "Sydney", 
              "FirstName": "Ben", 
              "PrimaryId": 101, 
              "LastName": "Swan", 
              "CarName": [
                  "Volkswagen", 
                  "Ford", 
                  "Audi"
              ], 
              "DogName": [
                  "Buddy", 
                  "Max"
              ]
          }, 
          {
              "City": "London", 
              "FirstName": "Julia", 
              "PrimaryId": 102, 
              "LastName": "Brown", 
              "CarName": [
                  "Mini"
              ], 
              "DogName": [
                  "Lucy"
              ]
          }
      ]
  }
  </code></pre>
  
  <p>Both <a href="https://stackoverflow.com/questions/29631711/python-to-parent-child-json">this post</a> and <a href="https://stackoverflow.com/questions/17043229/create-nested-json-from-csv">this one </a> have helped but I'm yet to create the correct structure.</p>