如何在不重复某些键 [Python] 的情况下在字典中创建列表?

How can i create a list in a dictionary without repeating certain keys [Python]?

我想使用 id_type_sale 和 sale_type_description 创建一个数组(查看输出中的 object_sale_types),保留密钥电子邮件、日期、order_id、存储但没有重复(假设它是相同的数据)。

输入

{
"105": [
    {
        "id_type_sale": 2,
        "email" : null,
        "date" : "2016-05-18",
        "order_id": 105,
        "sale_type_description": "Coffee shop",
        "store": "Ezio store"
    },
    {
        "id_type_sale": 5,
        "order_id": 105,
        "email" : null,
        "date" : "2016-05-18",
        "sale_type_description": "Book shop",
        "store": "Ezio store"
    }
],
"106": [
  {
      "id_type_sale": 3,
      "email" : null,
      "date" : "2016-05-19",
      "order_id": 106,
      "sale_type_description": "Food",
      "store": "Ezio store"
  },
  {
      "id_type_sale": 8,
      "order_id": 106,
      "email" : null,
      "date" : "2016-05-19",
      "sale_type_description": "Articles",
      "store": "Ezio store"
  }]}

输出预期

{
"105":[
    {
        "email":null,
        "date":"2016-05-18",
        "order_id":105,
        "store":"Ezio store",
        "object_sale_types":[
            {
                "id_type_sale":2,
                "sale_type_description":"Coffee shop"
            },
            {
                "id_type_sale":5,
                "sale_type_description":"Book shop"
            }
        ]
    }
],
"106":[
    {
        "email":null,
        "date":"2016-05-19",
        "order_id":106,
        "store":"Ezio store",
        "object_sale_types":[
            {
                "id_type_sale":3,
                "sale_type_description":"Food"
            },
            {
                "id_type_sale":8,
                "sale_type_description":"Articles"
            }
        ]
    }
]}

我该怎么办?什么是更好的方法?我想使用 python

首先,您需要使用 json.loads, then you can iterate over each (key, value) pair in the dictionary, building a new dictionary as you go with the common values from each value object and an array of the id_sale_type and sale_type_description values. Then you can output a new JSON using json.dumps:

加载 JSON 字符串
d = json.loads(j)
r = {}
for key, value in d.items():
    r[key] = { k : value[0][k] for k in ['email', 'date', 'order_id', 'store'] }
    r[key]['object_sales_types'] = [ { 'id_type_sale' : s['id_type_sale'], 'sale_type_description' : s['sale_type_description'] } for s in value]

print(json.dumps(r, indent=4))

输出:

{
    "105": {
        "email": null,
        "date": "2016-05-18",
        "order_id": 105,
        "store": "Ezio store",
        "object_sales_types": [
            {
                "id_type_sale": 2,
                "sale_type_description": "Coffee shop"
            },
            {
                "id_type_sale": 5,
                "sale_type_description": "Book shop"
            }
        ]
    },
    "106": {
        "email": null,
        "date": "2016-05-19",
        "order_id": 106,
        "store": "Ezio store",
        "object_sales_types": [
            {
                "id_type_sale": 3,
                "sale_type_description": "Food"
            },
            {
                "id_type_sale": 8,
                "sale_type_description": "Articles"
            }
        ]
    }
}