title: Create nested JSON from CSV
body: <p>I already read <a href="https://stackoverflow.com/questions/7948709/create-nested-json-from-flat-csv">Create nested JSON from flat csv</a>, but it didn't help in my case.</p>
  
  <p>I have quite a big spreadsheet created with Google Docs consisting of 11 rows and 74 columns (some columns are not occupied).</p>
  
  <p>I created an example on <a href="https://docs.google.com/spreadsheet/ccc?key=0ArAbcN96JXbzdEs4NzVETHVndGdneFZ6NzU4TE5zcWc" rel="nofollow noreferrer">Google Drive</a>. When exported as a <code>CSV</code> it looks like this:</p>
  
  <pre class="lang-none prettyprint-override"><code>id,name,email,phone,picture01,picture02,picture03,status
  1,Alice,alice@gmail.com,2131232,"image01_01
  [this is an image]",image01_02,image01_03,single
  2,Bob,bob@gmail.com,2854839,image02_01,"image02_02
  [description to image 2]",,married
  3,Frank,frank@gmail.com,987987,image03_01,image03_02,,single
  4,Shawn,shawn@gmail.com,,image04_01,,,single
  </code></pre>
  
  <p>Now I would like to have a <code>JSON</code> structure, which looks like this:</p>
  
  <pre><code>{
      "persons": [
          {
              "type": "config.profile",
              "id": "1",
              "email": "alice@gmail.com",
              "pictureId": "p01",
              "statusId": "s01"
          },
          {
              "type": "config.pictures",
              "id": "p01",
              "album": [
                  {
                      "image": "image01_01",
                      "description": "this is an image"
                  },
                  {
                      "image": "image_01_02",
                      "description": ""
                  },
                  {
                      "image": "image_01_03",
                      "description": ""
                  }
              ]
          },
          {
              "type": "config.status",
              "id": "s01",
              "status": "single"
          },
          {
              "type": "config.profile",
              "id": "2",
              "email": "bob@gmail.com",
              "pictureId": "p02",
              "statusId": "s02"
          },
          {
              "type": "config.pictures",
              "id": "p02",
              "album": [
                  {
                      "image": "image02_01",
                      "description": ""
                  },
                  {
                      "image": "image_02_02",
                      "description": "description to image 2"
                  }
              ]
          },
          {
              "type": "config.status",
              "id": "s02",
              "status": "married"
          }
      ]
  }
  </code></pre>
  
  <p>And so on for the other lines.</p>
  
  <p>My theoretical approach would be to go through the <code>CSV</code> file per row (here starts the first problem: now every row is equal to one line, but sometimes several, thus I need to count the commas?). Each row is equal to a block of <code>config.profile</code>, including the <code>id</code>, <code>email</code>, <code>pictureId</code>, and <code>statusId</code> (the latter two are being generated depending on the row number).</p>
  
  <p>Then for each row a <code>config.pictures</code> block is generated with the same <code>id</code> as the one inserted in the <code>config.profile</code> block. The <code>album</code> is an array of as many elements as pictures are given.</p>
  
  <p>Lastly each row has a <code>config.status</code> block, which, again, has the same <code>id</code> as the one given in <code>config.profile</code>, and one entry of <code>status</code> with the corresponding status.</p>
  
  <p>I'm entirely clueless how to create the nested and conditional JSON file.</p>
  
  <p>I just got to the point where I convert the <code>CSV</code> to valid <code>JSON</code>, without any nesting and additional info, which are not directly given in the <code>CSV</code>, like the <code>type</code>, <code>pictureId</code>, <code>statusId</code>, and so on.</p>
  
  <p>Any help is appreciated. If it is easier to program this in another script language (like <code>ruby</code>), I would gladly switch to those).</p>
  
  <p>Before someone thinks this is a homework or whatnot. It is not. I just want to automate an otherwise very tiresome copy&amp;paste task.</p>