Home > OS >  Combining duplicate key and value pairs in json using python
Combining duplicate key and value pairs in json using python

Time:10-07

I want to combine duplicate key and value pairs in JSON dictionaries. I am having trouble doing that. As other keys are not the same and are repetitive. I have the following JSON format:

[
   {
      "date":"2018-12-01",
      "9":{
         "mean":{
            "x_axis":-0.71,
            "y_axis":8.75,
            "z_axis":5.23
         }
      }
   },
   {
      "date":"2018-12-02",
      "4":{
         "mean":{
            "x_axis":-0.76,
            "y_axis":7.83,
            "z_axis":2.63
         }
      }
   },
   {
      "date":"2018-12-02",
      "5":{
         "mean":{
            "x_axis":0.59,
            "y_axis":6.46,
            "z_axis":7.12
         }
      }
   },
   {
      "date":"2018-12-02",
      "10":{
         "mean":{
            "x_axis":1.07,
            "y_axis":9.46,
            "z_axis":0.08
         }
      }
   }
]

But I want the following JSON format:

[
   {
      "date":"2018-11-22",
      "1":{
         "mean":{
            "x_axis":3.11,
            "y_axis":3.22,
            "z_axis":3.33
         }
      },
      "2":{
         "mean":{
            "x_axis":2.11,
            "y_axis":2.22,
            "z_axis":2.33
         }
      }
   },
   {
      "date":"2018-11-23",
      "1":{
         "mean":{
            "x_axis":3.11,
            "y_axis":3.22,
            "z_axis":3.33
         }
      },
      "2":{
         "mean":{
            "x_axis":2.11,
            "y_axis":2.22,
            "z_axis":2.33
         }
      }
   }
]

See that the same date key and values do not repeat. If the date is the same all other keys are under it. Please help!

CodePudding user response:

I would use the dict.update method to combine data entries as follows:

with open('path/to/file.json', 'r') as fp:
    data = json.load(fp)

combined = {}
for entry in data:
    date = entry['date']
    if date not in combined:
            combined[date] = {}

    # combine existing date dict with data in entry
    combined[date].update(entry)

    # delete 'date' key/value
    del combined[date]['date']

CodePudding user response:

You can identify your unique dates first and use that to create new dictionaries

# list_of_dicts is defined as list of dictionaries from your json file

dates = set([d['date'] for d in list_of_dicts])   # unique dates
dicts_by_date = [[d for d in list_of_dicts if d['date'] == date] for date in dates]  # create sublists based on date

new_list = [{k: v for d in ds for k, v in d.items()} for ds in dicts_by_date]

Output:

[
    {
        'date': '2018-12-01',
        '9': {'mean': {'x_axis': -0.71, 'y_axis': 8.75, 'z_axis': 5.23}}
    },
    {
        'date': '2018-12-02',
        '4': {'mean': {'x_axis': -0.76, 'y_axis': 7.83, 'z_axis': 2.63}},
        '5': {'mean': {'x_axis': 0.59, 'y_axis': 6.46, 'z_axis': 7.12}},
        '10': {'mean': {'x_axis': 1.07, 'y_axis': 9.46, 'z_axis': 0.08}}
    }
]
  • Related