devPie
devPie

Reputation: 75

merge common fields in the Json in python

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

Upvotes: 1

Views: 74

Answers (2)

user7864386
user7864386

Reputation:

Replace

mergedArr[i]['CommonTags'].append(d['CommonTags'])

with

mergedArr[i]['CommonTags'].extend(d['CommonTags'])
if not isinstance(mergedArr[i]['index'], list):
    mergedArr[i]['index'] = [mergedArr[i]['index']]
mergedArr[i]['index'].append(d['index'])

then your code will produce the desired outcome.

list.extend will let you extend the "CommonTags" value instead of appending a new list to it.

The other ugly if-condition will create a list if mergedArr[i]['index'] is not a list already and append to it.

Upvotes: 3

liveware
liveware

Reputation: 72

Changing the second append to extend will solve the issue, i.e. mergedArr[i]['CommonTags'].extend(d['CommonTags']). Extending the existing array is what you want, instead of appending a new array at the end.

Upvotes: 1

Related Questions