D
D
Dimsol1n02020-05-11 12:16:26
Python
Dimsol1n0, 2020-05-11 12:16:26

How to remove duplicates in json list?

I have this json:

{
    "items": [
        {
            "start_price": "3000",
            "end_price": "2850",
            "classid": "3106096757",
            "instanceid": "338584038",
            "name": "Souvenir MP5-SD | Lab Rats (Field-Tested)"
        },
        {
            "start_price": "4500",
            "end_price": "4275",
            "classid": "3106142260",
            "instanceid": "338584038",
            "name": "Souvenir MP5-SD | Lab Rats (Field-Tested)"
        },
        {
            "start_price": "5000",
            "end_price": "4750",
            "classid": "310781333",
            "instanceid": "302028390",
            "name": "P90 | Teardown (Field-Tested)"
        }
    ]
}

I need to sort by name, i.e. an object with the same "name" (duplicate): will be deleted. For example, from my json should remain:
{
    "items": [
        {
            "start_price": "4500",
            "end_price": "4275",
            "classid": "3106142260",
            "instanceid": "338584038",
            "name": "Souvenir MP5-SD | Lab Rats (Field-Tested)"
        },
        {
            "start_price": "5000",
            "end_price": "4750",
            "classid": "310781333",
            "instanceid": "302028390",
            "name": "P90 | Teardown (Field-Tested)"
        }
    ]
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vadim Shatalov, 2020-05-11
@netpastor

input_data = {
    "items": [
        {
            "start_price": "3000",
            "end_price": "2850",
            "classid": "3106096757",
            "instanceid": "338584038",
            "name": "Souvenir MP5-SD | Lab Rats (Field-Tested)"
        },
        {
            "start_price": "4500",
            "end_price": "4275",
            "classid": "3106142260",
            "instanceid": "338584038",
            "name": "Souvenir MP5-SD | Lab Rats (Field-Tested)"
        },
        {
            "start_price": "5000",
            "end_price": "4750",
            "classid": "310781333",
            "instanceid": "302028390",
            "name": "P90 | Teardown (Field-Tested)"
        }
    ]
}

output_data = [v for v in {inp['name']: inp for inp in input_data['items']}.values()]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question