title: How to create a JSON object in Ruby
body: <p>I am creating a JSON file with Ruby.
  I need to create the object "companies" within the JSON file.</p>
  
  <p>Expected result: </p>
  
  <pre><code>{"companies":[
      {\"label\":\"Wayfair \",\"values\":[54]},
      {\"label\":\"Move24 \",\"values\":[29]},
      {\"label\":\"Auto1 \",\"values\":[23]},
  ...
  ]}
  </code></pre>
  
  <p>What I am getting:</p>
  
  <pre><code>[  
     "{\"label\":\"Wayfair \",\"values\":[54,54]}",
     "{\"label\":\"Move24 \",\"values\":[29,29]}",
     "{\"label\":\"GetYourGuide \",\"values\":[28,28]}",
     "{\"label\":\"Auto1.com \",\"values\":[20,20]}", ...
  ]
  </code></pre>
  
  <p>My code:</p>
  
  <pre><code>data_hash = data_hash.map {|k,v| {label: k, values: v}}
  
  companies_json = []
  
  data_hash.each do |hash|
    companies_json &lt;&lt; hash.to_json
  end
  
  File.open('companies.json', 'w') do |f|
    f &lt;&lt; companies_json
  end
  </code></pre>