skip to Main Content

I have a situation where I need to read a json file containing statistics of different events coming into my system every 5 mins .I need to aggregate the counts for each of the events show against the events and pass it in another dictionary
This is my sample input dictionary json

"data": [
    {
      "recievedTime": "2023-04-10T00:05:00.000Z",
      "data": [
        {
          "EventType": "Call",
          "NumberOfEvents": 1000
        },
        {
          "EventType": "SMS",
          "NumberOfEvents": 55
        }
        ]}
    "recievedTime": "2023-04-10T00:10:00.000Z",
"data": [
        {
          "EventType": "Call",
          "NumberOfEvents": 2003
        },
        {
          "EventType": "SMS",
          "NumberOfEvents": 45
        }
        ]}
      ]

I want the output to take the sum of the event counts be in another json object like this:

"data" : [ {"Call": 3003,"SMS" : 100}]

How can I solve this problem ?

2

Answers


  1. You can use a loop to iterate through the events in the input dictionary and add up the counts for each event type. Here’s some example code to achieve this:

    import json
    
    input_data = {
        "data": [
            {
                "recievedTime": "2023-04-10T00:05:00.000Z",
                "data": [
                    {
                        "EventType": "Call",
                        "NumberOfEvents": 1000
                    },
                    {
                        "EventType": "SMS",
                        "NumberOfEvents": 55
                    }
                ]
            },
            {
                "recievedTime": "2023-04-10T00:10:00.000Z",
                "data": [
                    {
                        "EventType": "Call",
                        "NumberOfEvents": 2003
                    },
                    {
                        "EventType": "SMS",
                        "NumberOfEvents": 45
                    }
                ]
            }
        ]
    }
    
    output_data = {}
    
    for entry in input_data['data']:
        for event in entry['data']:
            event_type = event['EventType']
            event_count = event['NumberOfEvents']
            if event_type in output_data:
                output_data[event_type] += event_count
            else:
                output_data[event_type] = event_count
    
    output_json = json.dumps({'data': [output_data]})
    
    print(output_json)
    

    This will produce the desired output:

    {"data": [{"Call": 3003, "SMS": 100}]}
    
    Login or Signup to reply.
  2. It’s actually quite simple, you just need to us Python’s built-in json module to parse the JSON data, and then loop through the data array.

    You can do it like this

    import json
    
    input_data = {
      "data": [
        {
          "recievedTime": "2023-04-10T00:05:00.000Z",
          "data": [
            {
              "EventType": "Call",
              "NumberOfEvents": 1000
            },
            {
              "EventType": "SMS",
              "NumberOfEvents": 55
            }
          ]
        },
        {
          "recievedTime": "2023-04-10T00:10:00.000Z",
          "data": [
            {
              "EventType": "Call",
              "NumberOfEvents": 2003
            },
            {
              "EventType": "SMS",
              "NumberOfEvents": 45
            }
          ]
        }
      ]
    }
    
    event_counts = {}
    
    for item in input_data['data']:
        for event in item['data']:
            event_type = event['EventType']
            event_count = event['NumberOfEvents']
            if event_type not in event_counts:
                event_counts[event_type] = event_count
            else:
                event_counts[event_type] += event_count
    
    output_data = {"data": []}
    
    for event_type, event_count in event_counts.items():
        output_data["data"].append({event_type: event_count})
    
    print(json.dumps(output_data))
    

    Output

    {
      "data": [
        {
          "Call": 3003
        },
        {
          "SMS": 100
        }
      ]
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search