I have Some of the data that have nested attribute and need to combine and give new alias also remove the old one.
Sample Data:
JavaScript
x
17
17
1
[
2
{
3
"model.code":{
4
"model":{
5
"code":"Z15"
6
}
7
}
8
},
9
{
10
"model.name":{
11
"model":{
12
"name":"Test"
13
}
14
}
15
}
16
]
17
Expected Output:
JavaScript
1
9
1
[
2
{
3
"model":{
4
"code":"Z15",
5
"name":"Test"
6
}
7
}
8
]
9
How can acheived this using python
Advertisement
Answer
You can solve this by using jsonmerge
:
JavaScript
1
19
19
1
from jsonmerge import merge
2
3
d1 ={
4
"model.code":{
5
"model":{
6
"code":"Z15"
7
}
8
}
9
}
10
d2 = {
11
"model.name":{
12
"model":{
13
"name":"Test"
14
}
15
}
16
}
17
18
result = merge(d1["model.code"], d2["model.name"])
19
JavaScript
1
3
1
>>> result
2
{'model': {'code': 'Z15', 'name': 'Test'}}
3
for more complex use cases check Project Description.