You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Ruby on Rails中合并JSON数组:按月份累加收入

Merging Monthly Revenue Data in Ruby on Rails

To combine your JSON array by month (summing revenues and removing duplicate month entries), you can follow these straightforward steps using Ruby's built-in enumerables and Rails' JSON utilities:

Step 1: Parse the JSON into a Ruby Array

First, convert your raw JSON string into a Ruby array of hashes so we can manipulate it:

# Your original JSON string
json_input = '[{"revenu":0,"month":"January"}, {"revenu":0,"month":"February"}, {"revenu":0,"month":"March"}, {"revenu":0,"month":"April"}, {"revenu":1832.4430203602753,"month":"May"}, {"revenu":4502.1,"month":"May"}, {"revenu":54.673303657726436,"month":"May"}, {"revenu":0,"month":"June"}, {"revenu":0,"month":"July"}, {"revenu":0,"month":"August"}, {"revenu":0,"month":"September"}, {"revenu":0,"month":"October"}, {"revenu":0,"month":"November"}, {"revenu":0,"month":"December"}]'

# Parse JSON to Ruby array
raw_data = JSON.parse(json_input)

Step 2: Group Entries by Month

Use Ruby's group_by method to cluster all entries by their month value:

grouped_by_month = raw_data.group_by { |entry| entry["month"] }

This creates a hash where each key is a month name, and the value is an array of all entries associated with that month.

Step 3: Sum Revenue for Each Month

Iterate over each grouped month, calculate the total revenue, and build your merged dataset:

merged_data = grouped_by_month.map do |month, entries|
  {
    "month" => month,
    "revenu" => entries.sum { |entry| entry["revenu"].to_f }
  }
end
  • entries.sum adds up all the revenu values for the month. Using to_f ensures we handle any potential string values (a safe guard even though your input uses numeric values).
  • The result is an array of hashes with unique months and their aggregated revenue.

Step 4: Convert Back to JSON (Optional)

If you need the final result as a JSON string, use Rails' JSON.generate method:

merged_json = JSON.generate(merged_data)

Example Output

After running the above code, your merged data will look like this (formatted for readability):

[
  {"month":"January","revenu":0.0},
  {"month":"February","revenu":0.0},
  {"month":"March","revenu":0.0},
  {"month":"April","revenu":0.0},
  {"month":"May","revenu":6389.216324018002},
  {"month":"June","revenu":0.0},
  {"month":"July","revenu":0.0},
  {"month":"August","revenu":0.0},
  {"month":"September","revenu":0.0},
  {"month":"October","revenu":0.0},
  {"month":"November","revenu":0.0},
  {"month":"December","revenu":0.0}
]

Bonus: Optimizing for Database Data

If this data comes from a Rails model (e.g., a Transaction model with revenue and month attributes), you can perform grouping and summing directly in the database for better performance:

Transaction.group(:month).sum(:revenue)

This returns a hash like {"January"=>0, "May"=>6389.2163...}, which you can easily convert to your desired array format if needed.

内容的提问来源于stack exchange,提问作者shivam jindal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:43:14