Create aggregated values according to dates Jarray

Viewed 56

I am working on web api that takes data from database, does calculations and create JSON. I have done calculations and now have JObject that have similar structure to this one (simplified/name changes):

{
  "Cars": [
    {
      "CarName": "B19",
      "Models": [
        {
          "name": "B19_01",
          "values": {
          "date": ["2016-06-30","2016-07-30","2016-08-30", "2016-09-30"],
          "score": [0.1,0.2,0.35,0.59]
        },
        {
          "name": "B19_02",
          "values": {
          "date": ["2016-05-30","2016-06-30","2016-07-30","2016-08-30", "2016-09-30","2016-10-30"],
          "score": [0.05,0.1,0.21,0.38,0.49, 0.7]
        }]
    }
 ]}

          

What I am trying to do is, summing up the related fields according to time. The problem here is that the length of Jarrays are mostly different. I guess I should equalize all of them, add zero where dates are not present in "date" Jarray, so I can properly sum them according to the indexes. Final result should be something like this:

        {
          "CarName": "B19",
          "summed_values": {
          "date": ["2016-05-30","2016-06-30","2016-07-30","2016-08-30", "2016-09-30","2016-10-30"],
          "score": [0.05,0.2,0.41,0.73,1.08,0.7]
        }

What I have tried(maybe even don't need them): I implemented finding the max and minimum dates, and difference between them, and finally how many month to be added. But I cannot figure out what would be best practice for creating this aggregated Jarray? Simple code example, logic to approach the problem or resources that I can read would be highly appreciated.

0 Answers
Related