title: How do I merge multiple Hashes into a single valid JSON file?
body: <p>I'm using the following code to generate a JSON file containing all category information for a particular website.</p>
  
  <pre><code>require 'mechanize'
  
  @categories_hash = {}
  @categories_hash['category'] ||= {}
  @categories_hash['category']['id'] ||= {}
  @categories_hash['category']['name'] ||= {}
  @categories_hash['category']['group'] ||= {}
  @categories_hash['category']['search_attributes'] ||= {}
  
  # Initialize Mechanize object
  a = Mechanize.new
  
  # Open file and begin
  File.open("json/booyah/#{Time.now.strftime '%Y%m%d%H%M%S'}_booyah_categories.json", 'w') do |f|
    puts '# Writing category data to JSON file'
  
    # Begin scraping
    a.get('http://www.marktplaats.nl/') do |page|
      groups = page.search('//*[(@id = "navigation-categories")]//a')
      groups.each_with_index do |group, index_1|
  
        a.get(group[:href]) do |page_2|
          categories = page_2.search('//*[(@id = "category-browser")]//a')
          categories.each_with_index do |category, index_2|
  
            a.get(category[:href]) do |page_3|
              search_attributes = page_3.search('//*[contains(concat( " ", @class, " " ), concat( " ", "heading", " " ))]')
  
              search_attributes.each_with_index do |attribute, index_3|
                @categories_hash['category']['id'] = "#{index_1}_#{index_2}"
                @categories_hash['category']['name'] = category.text
                @categories_hash['category']['group'] = group.text
                @categories_hash['category']['search_attributes'][index_3] = attribute.text unless attribute.text == 'Outlet '
              end
  
              # Uncomment if you want to see what's being written
              puts @categories_hash['category'].to_json
  
              # Write the converted Hash to the JSON file
              f.write(@categories_hash['category'].to_json)
            end
          end
        end
      end
    end
  
    puts '|-----------&gt; Done.'
  
  end
  
  puts '# Finished.'
  </code></pre>
  
  <p>This code produces the following, <strong>invalid</strong> JSON file. Take a look at the full JSON file <a href="https://gist.github.com/narzero/7a1836c9f93750c63f14" rel="nofollow noreferrer">here</a>. It looks like this:</p>
  
  <pre><code>{
    "id": "0_0",
    "name": "Boeken en Bijbels",
    "group": "Antiek en Kunst",
    "search_attributes": {
      "0": "Prijs van/tot",
      "1": "Groep en Rubriek",
      "2": "Aangeboden sinds"
    }
  }{
    "id": "0_1",
    "name": "Emaille",
    "group": "Antiek en Kunst",
    "search_attributes": {
      "0": "Prijs van/tot",
      "1": "Groep en Rubriek",
      "2": "Aangeboden sinds"
    }
  }{
    "id": "0_2",
    "name": "Gereedschap en Instrumenten",
    "group": "Antiek en Kunst",
    "search_attributes": {
      "0": "Prijs van/tot",
      "1": "Groep en Rubriek",
      "2": "Aangeboden sinds"
    }
  }{...}
  </code></pre>
  
  <p>I want the output to be valid JSON and look like this:</p>
  
  <pre><code>[
    {
      "id": "0_0",
      "name": "Boeken en Bijbels",
      "group": "Antiek en Kunst",
      "search_attributes": {
        "0": "Prijs van/tot",
        "1": "Groep en Rubriek",
        "2": "Aangeboden sinds"
      }
    },
    {
      "id": "0_1",
      "name": "Emaille",
      "group": "Antiek en Kunst",
      "search_attributes": {
        "0": "Prijs van/tot",
        "1": "Groep en Rubriek",
        "2": "Aangeboden sinds"
      }
    },
    {
      "id": "0_2",
      "name": "Gereedschap en Instrumenten",
      "group": "Antiek en Kunst",
      "search_attributes": {
        "0": "Prijs van/tot",
        "1": "Groep en Rubriek",
        "2": "Aangeboden sinds"
      }
    },
    {...}
  ]
  </code></pre>
  
  <p>The question is, how do I accomplish this?</p>
  
  <h2>Update</h2>
  
  <p>A big thank you to <a href="https://stackoverflow.com/users/244128/maerics">maerics</a> for his <a href="https://stackoverflow.com/a/24295433/2246700">answer</a>.</p>
  
  <p>Here's the slightly updated, but working code:</p>
  
  <pre><code>require 'mechanize'
  
  @categories_hash = {}
  @categories_hash['category'] ||= {}
  @categories_hash['category']['id'] ||= {}
  @categories_hash['category']['name'] ||= {}
  @categories_hash['category']['group'] ||= {}
  @categories_hash['category']['search_attributes'] ||= {}
  
  @hashes = []
  
  # Initialize Mechanize object
  a = Mechanize.new
  
  # Begin scraping
  a.get('http://www.marktplaats.nl/') do |page|
    groups = page.search('//*[(@id = "navigation-categories")]//a')
    groups.each_with_index do |group, index_1|
  
      a.get(group[:href]) do |page_2|
        categories = page_2.search('//*[(@id = "category-browser")]//a')
        categories.each_with_index do |category, index_2|
  
          a.get(category[:href]) do |page_3|
            search_attributes = page_3.search('//*[contains(concat( " ", @class, " " ), concat( " ", "heading", " " ))]')
  
            search_attributes.each_with_index do |attribute, index_3|
              item = {
                id: "#{index_1}_#{index_2}",
                name: category.text,
                group: group.text,
                :search_attributes =&gt; {
                  :index_3.to_s =&gt; "#{attribute.text unless attribute.text == 'Outlet '}"
                }
              }
  
              @hashes &lt;&lt; item
  
              puts item
  
            end
          end
        end
      end
    end
  end
  
  # Open file and begin
  File.open("json/light/#{Time.now.strftime '%Y%m%d%H%M%S'}_light_categories.json", 'w') do |f|
    puts '# Writing category data to JSON file'
    f.write(@hashes.to_json)
    puts '|-----------&gt; Done.'
  end
  
  puts '# Finished.'
  </code></pre>