title: Create nested json like flare.json using ruby
body: <p>I wanted to create some visualizations using d3 which require me to build json. I have been doing it using ruby but since the json can be nested by more than 2 levels, I am having trouble to reduce the code duplication. Here is the code that I have got for 2 level nesting..</p>
  
  <pre><code>    level1, level2 = Array.new(2) { [] }
      array.each do |arrayItem|
          level1.push(arrayItem[:key1])
          level2.insert(-1, {
              label: arrayItem[:key2],
              parent: arrayItem[:key1],
              value: arrayItem[:value],
              })
      end 
      index1 = index2 = 0
      tempLevel1 = []
      main = Hash.new()
      main["name"] = "flare"
      main["children"] = []
      while( index1 &lt; level1.size ) do
          index2 = 0
          level1Value = level1[index1]
          level1Row = Hash.new()
          level1Row["name"] = level1Value
          level1Row["children"] = []
          tempLevel2 = []
          while( index2 &lt; level2.size ) do 
              level2Value = level2[index2][:label]
              level2Parent = level2[index2][:parent]
              level2Count = level2[index2][:value]
              if( level2Parent == level1Value )
                  level2Row = Hash.new()
                  if( level2Value.nil? )
                      level2Row["name"] = "Others"
                  else
                      level2Row["name"] = level2Value
                  end
                  level2Row["size"] = level2Count
                  tempLevel2 &lt;&lt; level2Row
              end
              index2 += 1
          end
          if( !tempLevel2.empty? )
              level1Row["children"] = level1Row["children"] | tempLevel2
              tempLevel1 &lt;&lt; level1Row
          end
          index1 += 1
      end
      main["children"] = main["children"] | tempLevel1
      return main
  </code></pre>
  
  <p>As you can imagine there can multiple levels and that would mean more nested loops in general and that would increase the code size too. Is there a way to reduce this code duplication and also the number of nested loops.</p>
  
  <p>The input array will be something like this ..</p>
  
  <pre><code>{:key1=&gt;"a", :key2=&gt;"p", :value=&gt;1}
  {:key1=&gt;"a", :key2=&gt;"q", :value=&gt;1}
  {:key1=&gt;"a", :key2=&gt;"r", :value=&gt;1}
  {:key1=&gt;"b", :key2=&gt;"s", :value=&gt;1}
  {:key1=&gt;"c", :key2=&gt;"q", :value=&gt;1}
  {:key1=&gt;"a", :key2=&gt;"t", :value=&gt;1}
  </code></pre>
  
  <p>And the output should be something like this ..</p>
  
  <pre><code>{
  "name": "flare",
  "children": [
      {
          "name": "a",
          "children": [
              {
                  "name": "p",
                  "size": 1
              },
              {
                  "name": "q",
                  "size": 1
              },
              {
                  "name": "r",
                  "size": 1
              },
              {
                  "name": "s",
                  "size": 1
              }
          ]
      },
      {
          "name": "b",
          "children": [
              {
                  "name": "t",
                  "size": 1
              }
          ]
      },
      {
          "name": "c",
          "children": [
              {
                  "name": "q",
                  "size": 1
              }
          ]
      }
  ]
  </code></pre>
  
  <p>}</p>