[使用Python基于ID元素加入两个JSON

问题描述 投票:0回答:1

我有两个JSON。一个有这样的条目:

one.json

"data": [
        {
            "results": {
                "counties": {
                    "32": 0, 
                    "96": 0, 
                    "12": 0
                }, 
                "cities": {
                    "total": 32, 
                    "reporting": 0
                }
            }, 
            "element_id": 999
        }, 

另一个具有这样的条目:

two.json

"data": [
        { 
            "year": 2020, 
            "state": "Virginia", 
            "entries": [
                {
                    "first_name": "Robert", 
                    "last_name": "Smith", 
                    "entry_id": 15723, 
                    "pivot": {
                        "county_id": 32, 
                        "element_id": 999
                    }
                }, 
                {
                    "first_name": "Benjamin", 
                    "last_name": "Carter", 
                    "entry_id": 15724, 
                    "pivot": {
                        "county_id": 34,
                        "element_id": 999
                    }
                }
            ], 
            "element_id": 999,
        },

我想基于element_id将one.json合并为two.json。 JSON有很多element_id,因此有一个元素可以找到要附加的合适元素。有没有一种方法可以基于append使用element_id来执行此操作,而不必使用for循环?上面第二个JSON的附加版本如下所示:

joined.json

"data": [
        { 
            "year": 2020, 
            "state": "Washington", 
            "entries": [
                {
                    "first_name": "Robert", 
                    "last_name": "Smith", 
                    "entry_id": 15723, 
                    "pivot": {
                        "county_id": 32, 
                        "element_id": 999
                    }
                }, 
                {
                    "first_name": "Benjamin", 
                    "last_name": "Carter", 
                    "entry_id": 15724, 
                    "pivot": {
                        "county_id": 34,
                        "element_id": 999
                    }
                }
            ], 
            "element_id": 999,
                {
                    "results": {
                        "counties": {
                            "32": 0, 
                            "96": 0, 
                            "12": 0
                    }, 
                    "cities": {
                        "total": 32, 
                        "reporting": 0
                    }
                },
        },

到目前为止我所拥有的:

for item in one:
    #this goes through one and saves each unique item in a couple variables
    temp_id = item["element_id"]
    temp_datachunk = item

    #then I try to find those variables in two and if I find them, I append
    for data in two:
        if data["element_id"] == temp_id:
            full_data = data.append(item)
            print(full_result)

现在,我的尝试死于append。我得到AttributeError: 'dict' object has no attribute 'append'

python json parsing merge append
1个回答
0
投票

类似这样的方法应该起作用:

source = '''
[{
    "results": {
        "counties": {
            "32": 0,
            "96": 0,
            "12": 0
        },
        "cities": {
            "total": 32,
            "reporting": 0
        }
    },
    "element_id": 999
}]
'''
target = """
[{
    "year": 2020,
    "state": "Virginia",
    "entries": [{
            "first_name": "Robert",
            "last_name": "Smith",
            "entry_id": 15723,
            "pivot": {
                "county_id": 32,
                "element_id": 999
            }
        },
        {
            "first_name": "Benjamin",
            "last_name": "Carter",
            "entry_id": 15724,
            "pivot": {
                "county_id": 34,
                "element_id": 999
            }
        }
    ],
    "element_id": 999
}]
"""

source_j = json.loads(source)
target_j = json.loads(target)
jsonpath_expr = parse('$..element_id')
source_match = jsonpath_expr.find(source_j)
target_match = jsonpath_expr.find(target_j)
if source_match[0].value==target_match[0].value:
    final = target_j+source_j
    print(final)

输出是组合的json。

© www.soinside.com 2019 - 2024. All rights reserved.