for a sample json as mentioned below
JSON =[{
"ID": "00300000-0000-0000-0000-000000000000",
"CommonTags": [
"Sports"
],
"subID": "149f43d0-6fa9-44f3-b4ba-6fb7a320d0a4",
"Description": "Sports,Arts",
"Title": "Biodata",
"index": 1,
"Value": "Availabity"
},
{
"ID": "00300000-0000-0000-0000-000000000000",
"CommonTags": [
"Social Media"
],
"subID": "149f43d0-6fa9-44f3-b4ba-6fb7a320d0a4",
"Description": "Sports,Arts",
"Title": "Biodata",
"index": 5,
"Value": "Availabity"
},
{
"ID": "00300000-0000-0000-0000-000000000079",
"CommonTags": [
"Sports"
],
"subID": "149f43d0-6fa9-44f3-b4ba-6fb7a320d0a4",
"Description": "Environmental Science",
"Title": "Biodata",
"index": 1,
"Value": "Performace"
}
]
I want to merger the Json fields CommonTags and index based on if the "Value" field is same for objects.
my approach
mergedArr=[]
def mergeCommon(value):
for i, d in enumerate(mergedArr):
if d['Value'] == value:
return i
return -1
for d in JSON:
if (i := mergeCommon(d['Value'])) < 0:
mergedArr.append(d)
else:
mergedArr[i]['CommonTags'].append(d['CommonTags'])
print(mergedArr)
I'm getting the common fields output to be a list within a list, but the expected output is to have all the elements in the single list and I'm not clear on how to append index values in a list
MY OUTPUT
[{
"ID": "00300000-0000-0000-0000-000000000000",
"CommonTags": ["Sports", ["Social Media"]],
"subID": "149f43d0-6fa9-44f3-b4ba-6fb7a320d0a4",
"Description": "Sports,Arts",
"Title": "Biodata",
"index": 1,
"Value": "Availabity"
}, {
"ID": "00300000-0000-0000-0000-000000000079",
"CommonTags": ["Sports"],
"subID": "149f43d0-6fa9-44f3-b4ba-6fb7a320d0a4",
"Description": "Environmental Science",
"Title": "Biodata",
"index": 1,
"Value": "Performace"
}]
EXPECTED OUTPUT
[{
"ID": "00300000-0000-0000-0000-000000000000",
"CommonTags": ["Sports", "Social Media"],
"subID": "149f43d0-6fa9-44f3-b4ba-6fb7a320d0a4",
"Description": "Sports,Arts",
"Title": "Biodata",
"index": [1, 5],
"Value": "Availabity"
}, {
"ID": "00300000-0000-0000-0000-000000000079",
"CommonTags": ["Sports"],
"subID": "149f43d0-6fa9-44f3-b4ba-6fb7a320d0a4",
"Description": "Environmental Science",
"Title": "Biodata",
"index": 1,
"Value": "Performace"
}]
Please Guide me on this. Thanks