Reputation: 1838
I have two json
files. One of them is a dictionary which is a subset of the other.
json_file_1.json
contains {'foo': 1, 'bar': 2, 'baz': 3}
json_file_2.json
contains {'foo': 100, 'bar': 200}
.
I want to create a final json
file that has the following: {'foo': 100, 'bar': 200, 'baz': 3}
Here is what I tried so far:
with open('json_file_1.json') as f1:
original_info = json.load(f1)
f1.close()
with open('json_file_2.json') as f2:
updated_info = json.load(f2)
f2.close()
print original_info # prints the correct dictionary
print updated_info # prints the correct dictionary
final_info = original_info.update(updated_info)
print final_info # prints None
with open('json_file_final.json', 'w+') as f_final:
json.dump(final_info, f_final)
However, when I open the final json
file, it only contains "Null". When I tried debugging it, I printed out original_info
and updated_info
, and they were each fine. I could call original_info.update(updated_info)
and that would produce a dictionary that was properly updated. However, it just isn't working for some reason when it's all put together?
Any thoughts?
Thanks so much!
Upvotes: 0
Views: 36
Reputation: 191728
dict.update
updates a dictionary in-place and returns None
.
You need to dump original_info
For reference,
In [11]: d1 = {'foo': 1, 'bar': 2, 'baz': 3}
In [12]: d2 = {'foo': 100, 'bar': 200}
In [13]: d3 = d1.update(d2)
In [14]: d3
In [15]: print(d3)
None
In [16]: d1
Out[16]: {'bar': 200, 'baz': 3, 'foo': 100}
Upvotes: 2