I have loaded two json files in Python3.8, and I need to merge the two based on a condition.
Obj1 = [{'account': '223', 'colr': '#555555', 'hash': True},
{'account': '134', 'colr': '#666666', 'hash': True},
{'account': '252', 'colr': '#777777', 'hash': True}]
Obj2 = [{'sn': 38796, 'code': 'df', 'id': 199, 'desc': 'jex - #777777- gg2349.252'},
{'sn': 21949, 'code': 'se', 'id': 193, 'desc': 'jex - #555555 - gf23569'},
{'sn': 21340, 'code': 'se', 'id': 3, 'desc': 'jex - #666666 - gf635387'}]
# What I am trying to get
Obj3 = [{'sn': 38796, 'code': 'df', 'id': 199, 'desc': 'jex - #777777- gg2349.252', 'account': '252', 'colr': '#777777', 'hash': True},
{'sn': 21949, 'code': 'se', 'id': 193, 'desc': 'jex - #555555 - gf23569', 'account': '223', 'colr': '#555555', 'hash': True},
{'sn': 21340, 'code': 'se', 'id': 3, 'desc': 'jex - #666666 - gf635387', 'account': '134', 'colr': '#666666', 'hash': True}]
I have tried from what I can gather everything on SO from append, extend etc but I fall short on the condition.
I need to be able to append elements in Obj1
to Obj2
at their correct place based on a condition that if the colr
of Obj1
is mentioned in desc
of Obj2
it should append that whole element from Obj1
into the correlated element of Obj2
. Or create a new Obj3
that I can print these updated values from.
What I have tried and looked at thus far Append JSON Object , Append json objects to nested list , Appending json object to existing json object and a few others that also didn't help.
Hope this makes sense and thank you
Something simple like this would work.
for i in range(len(Obj1)):
for j in range(len(Obj2)):
if Obj1[i]['colr'] in Obj2[j]['desc']:
Obj1[i].update(Obj2[j])
print(Obj1)
One approach is to first create a dictionary mapping each color to the JSON element. You can do this as
colr2elem = {elem['colr']: elem for elem in json_obj1}
Then you can see which color to append applying a Regular Expression to the description and update json_obj2
dictionary (merge dictionaries).
import re
for elem2 in json_obj2:
elem1 = colr2elem.get(re.search('#\d+', elem2['desc']).group(0))
elem2.update(elem1 if elem1 is not None else {})
Obj1 = [{'account': '223', 'colr': '#555555', 'hash': True},
{'account': '134', 'colr': '#666666', 'hash': True},
{'account': '252', 'colr': '#777777', 'hash': True}]
Obj2 = [{'sn': 38796, 'code': 'df', 'id': 199, 'desc': 'jex - #777777- gg2349.252'},
{'sn': 21949, 'code': 'se', 'id': 193, 'desc': 'jex - #555555 - gf23569'},
{'sn': 21340, 'code': 'se', 'id': 3, 'desc': 'jex - #666666 - gf635387'}]
Obj3 = []
for i in Obj1:
for j in Obj2:
if i["colr"]==j["desc"][6:13] :
a = {**j,**i}
Obj3.append(a)
print(Obj3)
You can use element1['colr'] in element2['desc']
to check if elements from the first and second arrays match. Now, you can iterate over the second array and for each of its elements find the corresponding element from the first array by checking this condition:
json_obj3 = []
for element2 in json_obj2:
for element1 in json_obj1:
if element1['colr'] in element2['desc']:
element3 = dict(**element1, **element2)
json_obj3.append(element3)
break # stop inner for loop, because matched element is found
BTW, this can be written as single expression using nested list comprehension :
json_obj3 = [
dict(**element1, **element2)
for element1 in json_obj1
for element2 in json_obj2
if element1['colr'] in element2['desc']
]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.