Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

python 3.x - How join/merge/update JSON dictionaries without overwriting data

I have a JSON list of dictionaries like so:

data = [{
    "title": "Bullitt",
    "release_year": "1968",
    "locations": "1153-57 Taylor Street",
    "fun_facts": "Embarcadero Freeway, which was featured in the film was demolished in 1989 because of structural damage from the 1989 Loma Prieta Earthquake)",
    "production_company": "Warner Brothers / Seven Arts
Seven Arts",
    "distributor": "Warner Brothers",
    "director": "Peter Yates",
    "writer": "Alan R. Trustman",
    "actor_1": "Steve McQueen",
    "actor_2": "Jacqueline Bisset",
    "actor_3": "Robert Vaughn",
    "id": 498
},
{
    "title": "Bullitt",
    "release_year": "1968",
    "locations": "John Muir Drive (Lake Merced)",
    "production_company": "Warner Brothers / Seven Arts
Seven Arts",
    "distributor": "Warner Brothers",
    "director": "Peter Yates",
    "writer": "Alan R. Trustman",
    "actor_1": "Steve McQueen",
    "actor_2": "Jacqueline Bisset",
    "actor_3": "Robert Vaughn",
    "id": 499
}]

How do I combine these dictionaries without overwriting the data?

So, the final result which I am trying to get is:

data = {
    "title": "Bullitt",
    "release_year": "1968",
    "locations": ["1153-57 Taylor Street", "John Muir Drive (Lake Merced)"]
    "fun_facts": "Embarcadero Freeway, which was featured in the film was demolished in 1989 because of structural damage from the 1989 Loma Prieta Earthquake)",
    "production_company": "Warner Brothers / Seven Arts
Seven Arts",
    "distributor": "Warner Brothers",
    "director": "Peter Yates",
    "writer": "Alan R. Trustman",
    "actor_1": "Steve McQueen",
    "actor_2": "Jacqueline Bisset",
    "actor_3": "Robert Vaughn",
    "id": 498, 499
}

I looked into merging JSON objects but all I came across was overwriting data. I do not want to overwrite anything. Not really sure how to approach this problem.

Would I have to make an empty list for the locations field and search through the entire data set looking for titles that are the same and take their locations and append them to the empty list and then finally update the dictionary? Or is there a better way/best practice when it comes to something like this?

question from:https://stackoverflow.com/questions/65516759/how-join-merge-update-json-dictionaries-without-overwriting-data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is one approach using a simple iteration.

Ex:

result = {}
tolook = ('locations', 'id')
for d in data:
    if d['title'] not in result:
        result[d['title']] = {k: [v] if k in tolook else v for k, v in d.items()}
    else:
        for i in tolook:
            result[d['title']][i].append(d[i])

print(result) # Or result.values()

Output:

{'Bullitt': {'actor_1': 'Steve McQueen',
             'actor_2': 'Jacqueline Bisset',
             'actor_3': 'Robert Vaughn',
             'director': 'Peter Yates',
             'distributor': 'Warner Brothers',
             'fun_facts': 'Embarcadero Freeway, which was featured in the film '
                          'was demolished in 1989 because of structural damage '
                          'from the 1989 Loma Prieta Earthquake)',
             'id': [498, 499],
             'locations': ['1153-57 Taylor Street',
                           'John Muir Drive (Lake Merced)'],
             'production_company': 'Warner Brothers / Seven Arts
Seven Arts',
             'release_year': '1968',
             'title': 'Bullitt',
             'writer': 'Alan R. Trustman'}}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...