Search code examples
pythonarraysjsondictionaryunique

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


I want to create an array (looking at object_sale_types in the output) using id_type_sale and sale_type_description, keeping the keys email, date, order_id, store but without repetition (assuming it's the same data).

Input

{
"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"
  }]}

Output expect

{
"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"
            }
        ]
    }
]}

How can I do? What's better approach? I'd like to use python


Solution

  • First you need to load your JSON string with 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:

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

    Output:

    {
        "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"
                }
            ]
        }
    }