如何用Python将AWS成本数据格式化为指定JSON结构?
Python实现AWS成本数据格式转换
需求说明
我有如下AWS成本数据(Python列表格式):
[ {'Keys': ['AWS Direct Connect'], 'Metrics': {'AmortizedCost': {'Amount': '0.758738186', 'Unit': 'USD'}}}, {'Keys': ['AWS Key Management Service'], 'Metrics': {'AmortizedCost': {'Amount': '0.000099', 'Unit': 'USD'}}}, {'Keys': ['EC2 - Other'], 'Metrics': {'AmortizedCost': {'Amount': '2556.9016995725', 'Unit': 'USD'}}}, {'Keys': ['Amazon Elastic Compute Cloud - Compute'], 'Metrics': {'AmortizedCost': {'Amount': '78956.164624688', 'Unit': 'USD'}}}, {'Keys': ['Amazon Elastic Load Balancing'], 'Metrics': {'AmortizedCost': {'Amount': '25.2072556293', 'Unit': 'USD'}}}, {'Keys': ['Amazon Elastic MapReduce'], 'Metrics': {'AmortizedCost': {'Amount': '6963.88', 'Unit': 'USD'}}}, {'Keys': ['Amazon Relational Database Service'], 'Metrics': {'AmortizedCost': {'Amount': '5263.199999968', 'Unit': 'USD'}}}, {'Keys': ['Amazon Route 53'], 'Metrics': {'AmortizedCost': {'Amount': '0.1', 'Unit': 'USD'}}}, {'Keys': ['Amazon Simple Queue Service'], 'Metrics': {'AmortizedCost': {'Amount': '0.0825688', 'Unit':'USD'}}}, {'Keys': ['Amazon Simple Storage Service'], 'Metrics': {'AmortizedCost': {'Amount': '9.9447263365', 'Unit': 'USD'}}}, {'Keys': ['Amazon Virtual Private Cloud'], 'Metrics': {'AmortizedCost': {'Amount': '46.56945646', 'Unit': 'USD'}}}, {'Keys': ['AmazonCloudWatch'], 'Metrics': {'AmortizedCost': {'Amount': '5.2942831629', 'Unit': 'USD'}}}, {'Keys': ['Tax'], 'Metrics': {'AmortizedCost': {'Amount': '0', 'Unit': 'USD'}}} ]
需要将其转换为如下JSON结构:
{"data": [ {"{#KEYS}": "AWS Direct Connect","{#AMOUNT}": "0.758738186"}, {"{#KEYS}": "AWS Key Management Service","{#AMOUNT}": "0.000099"}, ... ]}
实现步骤
- 遍历原始数据列表,逐个处理每个成本条目
- 从每个条目中提取
Keys数组的第一个元素(服务名称),以及Metrics下AmortizedCost的Amount值 - 将提取的信息整理成指定格式的字典,存入新列表
- 将最终的
{"data": 转换后列表}结构序列化为JSON字符串
完整Python代码
import json # 原始AWS成本数据 aws_cost_data = [ {'Keys': ['AWS Direct Connect'], 'Metrics': {'AmortizedCost': {'Amount': '0.758738186', 'Unit': 'USD'}}}, {'Keys': ['AWS Key Management Service'], 'Metrics': {'AmortizedCost': {'Amount': '0.000099', 'Unit': 'USD'}}}, {'Keys': ['EC2 - Other'], 'Metrics': {'AmortizedCost': {'Amount': '2556.9016995725', 'Unit': 'USD'}}}, {'Keys': ['Amazon Elastic Compute Cloud - Compute'], 'Metrics': {'AmortizedCost': {'Amount': '78956.164624688', 'Unit': 'USD'}}}, {'Keys': ['Amazon Elastic Load Balancing'], 'Metrics': {'AmortizedCost': {'Amount': '25.2072556293', 'Unit': 'USD'}}}, {'Keys': ['Amazon Elastic MapReduce'], 'Metrics': {'AmortizedCost': {'Amount': '6963.88', 'Unit': 'USD'}}}, {'Keys': ['Amazon Relational Database Service'], 'Metrics': {'AmortizedCost': {'Amount': '5263.199999968', 'Unit': 'USD'}}}, {'Keys': ['Amazon Route 53'], 'Metrics': {'AmortizedCost': {'Amount': '0.1', 'Unit': 'USD'}}}, {'Keys': ['Amazon Simple Queue Service'], 'Metrics': {'AmortizedCost': {'Amount': '0.0825688', 'Unit':'USD'}}}, {'Keys': ['Amazon Simple Storage Service'], 'Metrics': {'AmortizedCost': {'Amount': '9.9447263365', 'Unit': 'USD'}}}, {'Keys': ['Amazon Virtual Private Cloud'], 'Metrics': {'AmortizedCost': {'Amount': '46.56945646', 'Unit': 'USD'}}}, {'Keys': ['AmazonCloudWatch'], 'Metrics': {'AmortizedCost': {'Amount': '5.2942831629', 'Unit': 'USD'}}}, {'Keys': ['Tax'], 'Metrics': {'AmortizedCost': {'Amount': '0', 'Unit': 'USD'}}} ] # 转换数据格式 converted_data = [] for item in aws_cost_data: service_name = item['Keys'][0] cost_amount = item['Metrics']['AmortizedCost']['Amount'] converted_data.append({ "{#KEYS}": service_name, "{#AMOUNT}": cost_amount }) # 构建最终JSON结构并输出 result = {"data": converted_data} print(json.dumps(result, indent=2))
代码说明
- 使用Python内置的
json模块处理序列化,indent=2参数让输出的JSON格式更易读 - 通过键索引直接提取所需值,逻辑简洁高效
- 输出结果完全匹配目标JSON结构,可直接用于后续业务场景
内容的提问来源于stack exchange,提问作者Daniel Theis
相关产品推荐
相关产品推荐

