Forum Discussion
DougHenning1
10 months agoCommunity Manager
This code will filter the API_UserRoles output on a specified role and return the names as a semicolon-separated list:
{% set searchRoleId = '12' -%}
{% set users = b.json.qdbapi.users.user -%}
{% for user in users if user["@type"] == "user" -%}
{# Handle single vs array of roles -#}
{% if user.roles.role is iterable and user.roles.role is not mapping -%}
{# Array of roles -#}
{% set userRoles = user.roles.role | map(attribute='@id') | list -%}
{% else -%}
{# Single role, return as array of roles -#}
{% set userRoles = [user.roles.role['@id']] -%}
{% endif -%}
{% if searchRoleId in userRoles -%}
{{ user['name'] }}{{';' if not loop.last }}
{%- endif -%}
{% endfor -%}
Change line 1 to search for a different role id. The data returned from API_UserRoles is different if a user is a member of a single role vs multiple roles, so this code has the logic to handle both cases.
The output will look like "John Doe;Jane Smith;Bob Jones". The output of the regex step should assign a "group_0" to each of the names that you can iterate with a loop.
Hope that helps!