-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathjsonmerge.py
53 lines (42 loc) · 863 Bytes
/
jsonmerge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "ipetrash"
import json
json_str_1 = """
{
"Comment":"My comment",
"Count":10,
"Errors": null,
"DiskParam":
{
"DB":10.000000,
"DBAngle":1.234000
}
}
"""
json_str_2 = """
{
"Comment":"My super comment!",
"Count":20,
"Errors": "@##@##%!",
"DiskParam":
{
"DB":42,
"Foo": [1, 2, 3, 4]
}
}
"""
json_1 = json.loads(json_str_1)
json_2 = json.loads(json_str_2)
print(json_1)
print(json_2)
# TODO: нужно тестить, возможно, алгоритм где-то дырявый
def jsonmerge(j1, j2):
for k, v in j2.items():
if k in j1 and isinstance(j1[k], dict) and isinstance(v, dict):
jsonmerge(j1[k], v)
else:
j1[k] = v
jsonmerge(json_1, json_2)
print(json_1)
print(json.dumps(json_1, indent=4))