如果字典列表中的键相同,则合并嵌套的JSON

时间:2019-10-05 20:36:25

标签: python json

如果列表中的其他键相同,如何在value_map下将新时间戳作为嵌套json附加? 以下是字典的示例列表。

dicts = [{
    "key": "v3_CA125_2019-09-19",
    "sn": "M9257",
    "value_map": {
      "06:59:59": {
        "V_615": "1",
        "V_616": "2",
      }
    }
  },
  {
    "key": "v3_CA125_2019-09-19",
    "sn": "M9257",
    "value_map": {
      "10:59:59": {
        "V_613": "30",
        "V_614": "22"
      }
    }
  },
  {
    "key": "v3_CA125_2019-09-23",
    "sn": "M9257",
    "value_map": {
      "12:59:59": {
        "V_615": "33"
      }
    }
  },
  {
    "key": "v3_CA125_2019-09-23",
    "sn": "M9257",
    "value_map": {
      "09:59:59": {
        "V_602": "0.208984375"
      }
    }
  }
]

我尝试输出类似:

dicts = [{
    "key": "v3_CA125_2019-09-19",
    "sn": "M9257",
    "value_map": {
      "06:59:59": {
        "V_615": "1",
        "V_616": "2",
      },
      "10:59:59": {
        "V_613": "30",
        "V_614": "22"
      }
    }
  },
  {
    "key": "v3_CA125_2019-09-23",
    "sn": "M9257",
    "value_map": {
      "12:59:59": {
        "V_615": "33"
      },
      "09:59:59": {
        "V_602": "0.208984375"
      }
    }
  }
]

我尝试在value_map后面附加OrderedDict,但到目前为止还没有到。有什么建议吗?

from collections import OrderedDict
d = OrderedDict()
for l in dicts :
    d.setdefault((l['partition_key'], l['sort_key'], l['sn']), set()).add(l['value_map'])

result = [{'partition_key': k[0], 'sort_key': k[1], 'sn':k[2], 'value_map': v.pop() if len(v) == 1 else v} for k, v in d.items()] 

4 个答案:

答案 0 :(得分:1)

key_to_index_map ={} # to know when we saw the object with a given key
desired_list = []
for elem in dicts:
  key = elem['key']
  # append if you see another object with same key
  if key in key_to_index_map:
    value_map = desired_list[key_to_index_map[key]]["value_map"]
    value_map.update(elem['value_map']);
    desired_list[key_to_index_map[key]]['value_map'] = value_map;
  else: # or simply add to the final list
    desired_list.append(elem);
    key_to_index_map[key] = len(desired_list)-1;

答案 1 :(得分:1)

在需要字典时,您将value_map作为列表。

答案 2 :(得分:0)

您的value_map属性必须是数组[],而不是字典{}

答案 3 :(得分:0)

使用**运算符进行解压缩,即可在Python中“合并JSON”或合并两个dict对象的值。

在以下几行中,您可以找到一个简单的示例,您可以在其中观察行为并根据需要使用它。

d1 = {"a": 1, "b": 2}                                                                                                   

d2 = {"b": 3, "c": 4}                                                                                                   

d3 = {**d1, **d2}                                                                                                       

d3 # Output                                                                                                                      
{'a': 1, 'b': 3, 'c': 4}

d4 = {**d2, **d1}                                                                                                       

d4 # Output                                                                                                                     
{'b': 2, 'c': 4, 'a': 1}