Search code examples
jinja2

Remove objects with duplicate property


I have 'data' which is this array of objects:

{
"data": [
    {
        "name": "Erich",
        "fruit": "apple"
    },
    {
        "name": "Fred",
        "fruit": "orange"
    },
    {
        "name": "Erich",
        "fruit": "grape"
    },   
        "name": "Fred",
        "fruit": "banana"
    }
]}

Using only Jinja templating, I want to remove the objects with duplicate values for the "name" key, resulting in this:

{
"data": [
    {
        "name": "Erich",
        "fruit": "apple"
    },
    {
        "name": "Fred",
        "fruit": "orange"
    }
]}

At this point I don't care which of the duplicate objects is removed (e.g. apple vs grape or orange vs. banana) but if there is a way to use some logic with another KVP to decide, I would be interested in knowing how.


Solution

  • You can use the unique filter:

    {% for item in data|unique(attribute='name') %}
    {{ item }}
    {% endfor %}
    

    You can see that in action in the following code:

    import jinja2
    
    t = jinja2.Template(
        """
    {% for item in data|unique(attribute='name') %}
    {{ item }}
    {% endfor %}
    """
    )
    
    context = {
        "data": [
            {"name": "Erich", "fruit": "apple"},
            {"name": "Fred", "fruit": "orange"},
            {"name": "Erich", "fruit": "grape"},
            {"name": "Fred", "fruit": "banana"},
        ]
    }
    
    print(t.render(**context))
    

    Which prints out:

    
    
    {'name': 'Erich', 'fruit': 'apple'}
    
    {'name': 'Fred', 'fruit': 'orange'}