json 将值从一个字典转换到另一个字典作为Python中的key:value

h5qlskok  于 2023-04-22  发布在  Python
关注(0)|答案(2)|浏览(171)

我有一个情况,我需要读取一个json文件,其中包含每5分钟进入我的系统的不同事件的统计信息。我需要聚合每个事件的计数,并将其传递到另一个字典中。这是我的示例输入字典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
        }
        ]}
      ]

我希望输出将事件计数的总和放在另一个json对象中,如下所示:

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

我该如何解决这个问题?

rnmwe5a2

rnmwe5a21#

您可以使用循环来遍历输入字典中的事件,并将每个事件类型的计数相加。下面是实现此目的的一些示例代码:

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)

这将产生所需的输出:

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

bd1hkmkf2#

实际上很简单,你只需要使用Python内置的json模块来解析JSON数据,然后循环遍历数据数组。
你可以这样做

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))

输出

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

相关问题