title: Read values from JSON objects and sum all values into single object using Ruby
body: <p>I want to add the values "V" from the response objects based on Time's <code>at</code>, into single object. We will get different responses with single or more than one objects. For example, the following data has three objects after parsed the response : </p>
  
  <pre><code>data = [
    {
      "MeterId"=&gt;"201_12",
      "MeterName"=&gt;"WHr",
      "Values"=&gt; [
        {"at"=&gt;"2015-11-25 01:00:00", "v"=&gt;"90.00"}, 
        {"at"=&gt;"2015-11-25 01:10:00", "v"=&gt;"10.00"}
      ]
    },
    {
      "MeterId"=&gt;202_12",
      "MeterName"=&gt;"WHr",
      "Values"=&gt;[
        {"at"=&gt;"2015-11-25 01:00:00", "v"=&gt;"50.00"}, 
        {"at"=&gt;"2015-11-25 01:10:00", "v"=&gt;"60.00"}
      ]
    },
    {
      "MeterId"=&gt;"203_12",
      "MeterName"=&gt;"WHr",
      "Values"=&gt;[
        {"at"=&gt;"2015-11-25 01:00:00", "v"=&gt;"70.00"}, 
        {"at"=&gt;"2015-11-25 01:10:00", "v"=&gt;"40.00"}
      ]
    }
  ]
  </code></pre>
  
  <p>Actual Output:</p>
  
  <pre><code>#&lt;DataObject:0x00000002e3fee8@parameter="RefPowerSubscription", 
  @samples=[
  #&lt;DataHash:0x00000002e3fda8 @value="90.00", @time_stamp="2015-11-25 01=&gt;00=&gt;00", @count=0, @name=""&gt;, 
  #&lt;DataHash:0x00000002e3fd30 @value="10.00", @time_stamp="2015-11-25 01=&gt;10=&gt;00", @count=0, @name=""&gt;, 
  #&lt;DataHash:0x00000002e3fc90 @value="50.00", @time_stamp="2015-11-25 01=&gt;00=&gt;00", @count=0, @name=""&gt;, 
  #&lt;DataHash:0x00000002e3fc18 @value="60.00", @time_stamp="2015-11-25 01=&gt;10=&gt;00", @count=0, @name=""&gt;, 
  #&lt;DataHash:0x00000002e3fb78 @value="70.00", @time_stamp="2015-11-25 01=&gt;00=&gt;00", @count=0, @name=""&gt;, 
  #&lt;DataHash:0x00000002e3fb00 @value="40.00", @time_stamp="2015-11-25 01=&gt;10=&gt;00", @count=0, @name=""&gt;], 
  @max=0, @min=0, @sum=0&gt;
  </code></pre>
  
  <p>I need output in this format:</p>
  
  <pre><code>#&lt;DataObject:0x00000002eb85f0 @parameter="RefPowerSubscription", 
  @samples=[
  #&lt;DataHash:0x00000002eb84b0 @value="210.00", @time_stamp="2015-11-25 01=&gt;00=&gt;00", @count=0, @name=""&gt;,
  #&lt;DataHash:0x00000002eb8208 @value="110.00", @time_stamp="2015-11-25 01=&gt;10=&gt;00", @count=0, @name=""&gt;], 
  @max=0, @min=0, @sum=0&gt;
  </code></pre>
  
  <p>This is my code. It is working with one object but I don't know how to handle multiple objects. Here DataObject is class, we are using it to store Hash values.Can anyone help me out with logic? </p>
  
  <pre><code>class DataObject
    attr_accessor :count, :samples, :average, :parameter
  
    def initialize(parameter,sample_size = 0)
      @parameter = parameter
      @samples = []
  
      sample_size.times do
        @samples &lt;&lt; DataHash.new
      end
  
      @max = 0
      @min = 0
      @sum = 0
    end
  end
  </code></pre>
  
  
  
  <pre><code>class DataHash
    attr_accessor :value, :time_stamp, :count , :name
  
    def initialize
      @value = 0
      @time_stamp = ''
      @count = 0
      @name = ''
    end
  end
  </code></pre>
  
  
  
  <pre><code>dobj = DataObject.new('RefPowerSubscription')
  dhash = DataHash.new
  len = data.length
  
  until i == len do 
    data[i]['Values'].each do | data |
      dhash.time_stamp = data['at']
      dhash.value = data['v']
      dobj.samples &lt;&lt; dhash.dup
    end
  
    i += 1
  end
  
  p dobj
  </code></pre>
  
  <p>Working Code </p>
  
  <pre><code>vtime = Time.parse(stime).to_s
  i =0
  loop do
  value = 0
  
  data.reduce(len) do | memo, temp |
  
  temp['Values'].reduce(temp.size) do | sum , order|
      if Time.parse(vtime) == Time.parse(order['at'])
          value += order['v'].to_i
      end
  end
  
  end
  dhash.time_stamp = Time.parse(vtime)  # order['at']
  dhash.value = value 
  dobj.samples[i] = dhash.clone
  
  vtime = (Time.parse(vtime) + 600 ).to_s
  i +=1
  break if Time.parse(vtime) &gt; Time.parse(etime)
  end
  
  p dobj
  </code></pre>