Reputation: 28783
I have some JSON data that stores a date time string and total. e.g.
[{
"Date": "2012-04-01 12:00:00",
"Total": "14"
}, {
"Date": "2012-04-02 06:00:00",
"Total": "3"
}, {
"Date": "2012-04-02 14:00:00",
"Total": "12"
}, {
"Date": "2012-04-02 16:00:00",
"Total": "5"
}, {
"Date": "2012-04-02 17:00:00",
"Total": "7"
}, {
"Date": "2012-04-03 06:00:00",
"Total": "9"
}, {
"Date": "2012-04-03 14:00:00",
"Total": "2"
}, {
"Date": "2012-04-04 06:00:00",
"Total": "1"
}, {
"Date": "2012-04-04 14:00:00",
"Total": "10"
}, {
"Date": "2012-04-04 19:00:00",
"Total": "8"
}, {
"Date": "2012-04-04 21:00:00",
"Total": "4"
}]
What I'd like to do is create new JSON objects for data that is the same year, month, and day.
So for example a full set of data could be formatted into:
DAY:
[{
"Date": "2012-04-01"
"Total": "3"
},{
"Date": "2012-04-02"
"Total": "4"
},{
"Date": "2012-04-03"
"Total": "6"
}]
MONTH:
[{
"Date": "2012-04"
"Total": "36"
},{
"Date": "2012-05"
"Total": "11"
},{
"Date": "2012-06"
"Total": "23"
},{
"Date": "2012-07"
"Total": "17"
}]
YEAR:
[{
"Date": "2012"
"Total": "91"
},{
"Date": "2013"
"Total": "102"
},{
"Date": "2014"
"Total": "78"
}]
How could I do this is Rails? If I could get an example of how I could do one of them, then I could then use that as a base to do the others!
So in the controller I have:
def get_count_day
render json: data.to_json
end
def get_count_month
render json: data.to_json
end
def get_count_year
render json: data.to_json
end
Upvotes: 1
Views: 822
Reputation: 1940
Hey you can try this way for Day
given_json.group_by{|b| b["Date"].to_date.strftime("%Y-%d-%m")}.collect{|key,value| {"Date" =>key , "Total" => value.sum{|d| d["Total"].to_i}}}
For Month
given_json.group_by{|b| b["Date"].to_date.strftime("%Y-%m")}.collect{|key,value| {"Date" =>key , "Total" => value.sum{|d| d["Total"].to_i}}}
For Year
given_json.group_by{|b| b["Date"].to_date.strftime("%Y")}.collect{|key,value| {"Date" =>key , "Total" => value.sum{|d| d["Total"].to_i}}}
Upvotes: 1
Reputation: 23661
count_by_day =
json.group_by{ |record| record[:Date].to_date.strftime }
.map { |k, v| { Date: k, Total: v.map { |y| y[:Total].to_i }.sum.to_s } }
#=> [{:Date=>"2012-04-01", :Total=>"14"}, {:Date=>"2012-04-02", :Total=>"27"}, {:Date=>"2012-04-03", :Total=>"11"}, {:Date=>"2012-04-04", :Total=>"23"}]
count_by_month =
json.group_by{ |record| record[:Date].to_date.strftime('%Y-%m') }
.map { |k, v| { Date: k, Total: v.map { |y| y[:Total].to_i }.sum.to_s } }
#=> [{:Date=>"2012-04", :Total=>"75"}]
count_by_year =
json.group_by{ |record| record[:Date].to_date.strftime('%Y') }
.map { |k, v| { Date: k, Total: v.map { |y| y[:Total].to_i }.sum.to_s } }
#=> [{:Date=>"2012", :Total=>"75"}]
Upvotes: 2
Reputation: 5528
Here is an example to retrieve them grouped by day:
grouped_json = Hash.new(0)
your_json_array.each do |array_element|
date = Date.parse(array_element['Date']).strftime('%Y-%m-%d')
grouped_json[date] += array_element['Total']
end
grouped_json.map {|k,v| {'Date' => k, 'Total' => v} }
by month?
grouped_json = Hash.new(0)
your_json_array.each do |array_element|
date = Date.parse(array_element['Date']).strftime('%Y-%m')
grouped_json[date] += array_element['Total']
end
grouped_json.map {|k,v| {'Date' => k, 'Total' => v} }
by year?
grouped_json = Hash.new(0)
your_json_array.each do |array_element|
date = Date.parse(array_element['Date']).strftime('%Y')
grouped_json[date] += array_element['Total']
end
grouped_json.map {|k,v| {'Date' => k, 'Total' => v} }
Upvotes: 0