What I’m lloking to get is :
From:
arrayList = [{
"value": 10,
"name": "BBB",
"extra": "sample"
},{
"value": 10,
"name": "BBB",
}, {
"value": 10,
"name": "AAA",
"extra": "sample"
}, {
"value": 10,
"name": "AAA",
}
]
Based on the selected nameKey and valueKey, deduplication list + sum up values
So if input values will be: nameKey = "name" and valueKey = "value"
Output will be:
arrayList = [{
"value": 20,
"name": "BBB",
"extra": "sample"
}, {
"value": 20,
"name": "AAA",
"extra": "sample"
}
]
And if input values will be: nameKey = "extra" and valueKey = "value"
Output will be:
arrayList = [{
"value": 20,
"name": ["BBB","AAA"], <- If keys are the same, them they will be added to array
"extra": "sample"
},{
"value": 10,
"name": "BBB",
}, {
"value": 10,
"name": "AAA",
}
]
For the simplededuplication currently I’m using this: [dict(t) for t in {tuple(d.items()) for d in arrayList }]
But not sure how to do they way I have mentioned above, as it is sounds too complicated to make it works like this
2
Answers
Not too sure if this is what you was asking for but here is my assumption:
my approach in solving this simple, but depends on JsonDF package that I was working on, which gives you control over Json.
I will suggest to make a Json Template and insert to it the values, every time you insert search fot the ‘key’ which you want to find dublicates with, and from the Json that have that key you can get other values and compate it, and make the summtion and update the Json Values.
the code will be something like this :
this will do the trick for you
you can find the packge on GitHub
or install it via
pip install JsonDF