Home > Software engineering >  Sorting Data from a JSON file by date and add up the values for each day
Sorting Data from a JSON file by date and add up the values for each day

Time:12-30

I have the following problem: My JSON file that I get from my online shop is structured like this:

{
   "orders":[
      {
         "total_price":"100.10",
         "updated_at":"2022-12-29T12:10:39 01:00"
      },
      {
         "total_price":"50.20",
         "updated_at":"2022-12-29T12:05:02 01:00"
      },
      {
         "total_price":"53.20",
         "updated_at":"2022-12-29T12:00:39 01:00"
      },
      {
         "total_price":"50.00",
         "updated_at":"2022-12-28T11:54:29 01:00"
      },
      {
         "total_price":"30.00",
         "updated_at":"2022-12-27T13:35:03 01:00"
      }
   ]
}

I would like to filter all orders by date and add the numbers up. On one day, several orders can arise and these should then be added to a single total number. From this a new JSON should be created which looks like this.

For each day a field, if there is no data, then total_*day_*sales are zero.

{
   "day":[
      {
         "total_day_sales":"0.00",
         "day_in_themonth":"1"
      },
      {
         "total_day_sales":"0.00",
         "day_in_themonth":"2"
      },
      {
         "total_day_sales":"0.00",
         "day_in_themonth":"3"
      },
      {
         "total_day_sales":"30.00",
         "day_in_themonth":"27"
      },
      {
         "total_day_sales":"50.00",
         "day_in_themonth":"28"
      },
      {
         "total_day_sales":"203.50",
         "day_in_themonth":"29"
      }
   ]
}

I already tried to create loops and sort the data by "sorted(jsondata, key=lambda x: ...."
But, to combine the values from the same day is a problem. My idea was to create a range loop, where the loop searching for the "day number" value in the string 2022-12-28T11:54:29 01:00, but I lose the total_price value in the process.

I need some hints where I can go to extract this data and create a new JSON based on the example.

Thank you very much

Some of the first ideas:

alist = json.loads(jsondump)

newlist = []

for it in alist['orders']:
    newlist.append(it['updated_at'])
    
newlist.sort()

for n in newlist:
    print(n)

CodePudding user response:

For each object, you could extract the day, and compute the totals in a map, for example:

# initialize all day totals to 0
days_to_totals = {f"{day:02}": 0 for day in range(1, 32)}

for item in jsondump['orders']:
    day = item['updated_at'][8:10]
    days_to_totals[day]  = float(item['total_price'])

print(days_to_totals)
# {'01': 0, '02': 0, '03': 0, ..., '27': 30.0, '28': 50.0, '29': 203.5, '30': 0, '31': 0}
  • Related