title: CSV to JSON using BASH
body: <p>I am trying to covert the below csv into json format.</p>
  
  <pre><code>Africa,Kenya,NAI,281
  Africa,Kenya,NAI,281
  Asia,India,NSI,100
  Asia,India,BSE,160
  Asia,Pakistan,ISE,100
  Asia,Pakistan,ANO,100
  European Union,United Kingdom,LSE,100
  </code></pre>
  
  <p>This is the desired json format and I just cannot get to create it. I will post my work in progress below this.. Any help or direction would be appreciated...</p>
  
  <pre><code>  {"name":"Africa",
        "children":[
        {"name":"Kenya",
            "children":[
            {"name":"NAI","size":"109"},
            {"name":"NAA","size":"160"}]}]},
    {"name":"Asia",
        "children":[
        {"name":"India",
            "children":[
            {"name":"NSI","size":"100"},
            {"name":"BSE","size":"60"}]},
    {"name":"Pakistan",
        "children":[
        {"name":"ISE","size":"120"},
        {"name":"ANO","size":"433"}]}]},
    {"name":"European Union",
          "children":[
          {"name":"United Kingdom",
              "children":[
              {"name":"LSE","size":"550"},
              {"name":"PLU","size":"123"}]}]}
  </code></pre>
  
  <p>Work in Progress.</p>
  
  <p>$1 is the file with the csv values pasted above.</p>
  
  <pre><code>#!/bin/bash
  
  pcountry=$(head -1 $1 | cut -d, -f2)
  
  cat $1 | while read line ; do 
  
  region=$(echo $line|cut -d, -f1)
  country=$(echo $line|cut -d, -f2)
  code=$(echo $line|cut -d, -f3-)
  size=$(echo $line|cut -d, -f4)
  
  if test "$pcountry" == "$country" ;
    then 
    echo -e {\"name\":\"$region\", '\n' \"children\": [ '\n'{\"name\":\"$country\",'\n'\"children\": [ '\n' \{\"name\":\"NAI\",\"size\":\"$size\"\}
    else
        if test "$pregion" == "$region"
        then :
        else 
            echo -e ,'\n'{\"name\":\""$region\", '\n' \"children\": [ '\n'{\"name\":\"$country\",'\n'\"children\": [ '\n' \{\"name\":\"NAI\",\"size\":\"$size\"\},
  
  
  pcountry=$country
  pregion=$region
  
  fi ; done
  </code></pre>
  
  <h1>Problem is that I cannot seem to find a way to find out when a countries value ends.</h1>