1

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

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

thanks for guiding me on that, can you please also guide me with the approach of appending the index values(int values) into a list . where all the conditions remain same. Thanks
See the the response below by enke. You need either an if statement or a try/except to deal with the putting the "index" integers in a list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.