Forum Discussion
DougHenning1
2 years agoCommunity Manager
Hey Shane, here's a quick example using the Text step:
------------------------------
Doug Henning
------------------------------
{% set users = [
{
"id": 1,
"name": "bob",
"city": "Boston"
},
{
"id": 2,
"name": "jane",
"city": "Boston"
},
{
"id": 3,
"name": "george",
"city": "Baton Rouge"
}
] %}
{{ users | groupby("city") }}
The results in the activity log:
text:"[(u'Baton Rouge', [{u'city': u'Baton Rouge', u'id': 3, u'name': u'george'}]), (u'Boston', [{u'city': u'Boston', u'id': 1, u'name': u'bob'}, {u'city': u'Boston', u'id': 2, u'name': u'jane'}])]"
As you can see, the list is now grouped by the city, with each city results in a nested list.
The example on the Jinja website works to parse this sample data. Remove the last line from the Jinja above and add this to the end:
<ul>{% for city, items in users|groupby("city") -%}
<li>{{ city }}
<ul>{% for user in items -%}
<li>{{ user.name }}
{% endfor -%}</ul>
</li>
{% endfor -%}</ul>
The outer loop references the first value as 'city' and the second value as 'items' which is the list for that city. The inner loop iterates the items for that city.
Hope that helps!
------------------------------
Doug Henning
------------------------------