Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove duplicates in an object in JSONiq

This is an example object that I got:

{ "query1" : [ { "name" : "John", "id" : 1234 }, { "name" : "Rose", "id" : 3214 }, { "name" : "John", "id" : 1234 } ] }

How can I remove the duplicates using group by and array navigation / unboxing?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried implementing the group by clause after the where clause but did not get the correct answer

>Solution :

For remove duplicates item from Json object you can use this code:

from collections import OrderedDict

data = {
    "query1": [
        {"name": "John", "id": 1234},
        {"name": "Rose", "id": 3214},
        {"name": "John", "id": 1234},
    ]
}

unique_data = {}
for key, array in data.items():
    unique_objects = OrderedDict()
    for obj in array:
        unique_objects[(obj["name"], obj["id"])] = obj
    unique_data[key] = list(unique_objects.values())

print(unique_data)

Result:

{'query1': [{'name': 'John', 'id': 1234}, {'name': 'Rose', 'id': 3214}]}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading