Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用相同的键合并JSON元素_Python_Json - Fatal编程技术网

Python 使用相同的键合并JSON元素

Python 使用相同的键合并JSON元素,python,json,Python,Json,我一直在努力使用Python进行JSON转换。我有以下格式的JSON: { "Children": [{ "child": "Child 0"}], "Parent": "Parent 10" }, { "Children": [{ "child": "Child 1"}], "Parent": "Parent 10" }, { "Children": [{ "child": "Child 2"}], "Parent": "Parent 11" }

我一直在努力使用Python进行JSON转换。我有以下格式的JSON:

{
    "Children": [{ "child": "Child 0"}],
    "Parent": "Parent 10"
},
{
    "Children": [{ "child": "Child 1"}],
    "Parent": "Parent 10"
},
{
    "Children": [{ "child": "Child 2"}],
    "Parent": "Parent 11"
},
但是,我不想有重复的父母,我想把孩子们合并在一起,得到:

{
    "Children": [{ "child": "Child 0"}, { "child": "Child 1"}],
    "Parent": "Parent 10"
},
{
    "Children": [{ "child": "Child 2"}],
    "Parent": "Parent 11"
},

看看itertools groupby函数。下面是一个按父项分组数据的示例

>>> from itertools import groupby
>>> import pprint

>>> data = [{
    "Children": [{ "child": "Child 0"}],
    "Parent": "Parent 10"
},
{
    "Children": [{ "child": "Child 1"}],
    "Parent": "Parent 10"
},
{
    "Children": [{ "child": "Child 2"}],
    "Parent": "Parent 11"
}]

>>> data_grouped = {k: list(v) for k, v in groupby(data, key=lambda x: x["Parent"])}

>>> pp = pprint.PrettyPrinter(indent=4)
>>> pp.pprint(data_grouped)

{   'Parent 10': [   {   'Children': [{'child': 'Child 0'}],
                         'Parent': 'Parent 10'},
                     {   'Children': [{'child': 'Child 1'}],
                         'Parent': 'Parent 10'}],
    'Parent 11': [{'Children': [{'child': 'Child 2'}], 'Parent': 'Parent 11'}]}
在这里,我将您的示例dict放在一个列表中,并按每个dict中的父条目进行分组。这些都包含在dict理解中,以提供有意义的输出。

您也可以使用a来完成此操作,它可以在最后序列化:

from collections import defaultdict
from json import dumps

data = [
    {"Children": [{"child": "Child 0"}], "Parent": "Parent 10"},
    {"Children": [{"child": "Child 1"}], "Parent": "Parent 10"},
    {"Children": [{"child": "Child 2"}], "Parent": "Parent 11"},
]

d = defaultdict(list)
for dic in data:
    parent, children = dic["Parent"], dic["Children"]
    d[parent].extend(children)

result = []
for k, v in d.items():
    result.append({"Parent": k, "Children": v})

print(dumps(result))
它给出了JSON对象的JSON数组:

[{"Parent": "Parent 10", "Children": [{"child": "Child 0"}, {"child": "Child 1"}]}, {"Parent": "Parent 11", "Children": [{"child": "Child 2"}]}]
还可以使用嵌套的
defaultdict()
,按父键将数据分组:

这就形成了这种新的结构:

{"Parent 10": {"Children": [{"child": "Child 0"}, {"child": "Child 1"}]}, "Parent 11": {"Children": [{"child": "Child 2"}]}}

并允许轻松查找父对象的O(1)

你试过什么,到底有什么问题?“挣扎”意味着你可以尝试一下。第一步是读取数据,解析JSON,这样你就可以得到一个Python内部表示的字典。然后,编写代码进行转换,并再次将整个文件格式化为JSON。
{"Parent 10": {"Children": [{"child": "Child 0"}, {"child": "Child 1"}]}, "Parent 11": {"Children": [{"child": "Child 2"}]}}