I have a JSON file with team members’ information. I would like to display only those who match certain criteria using a shortcode. It should check whether a given string (here: ga
) is part of an array (here: roles
) in this JSON file.
This is the file /data/people/people.json
:
[
{
"name": "John Doe",
"roles": [
"president",
"ga"
]
},
{
"name": "Max Mustermann",
"roles": [
"manager-programme"
]
}
]
This is the shortcode file /layout/shortcodes/people_list.html
:
{{ $json := getJSON "data/people/people.json" }}
<!-- show all members of a certain role -->
{{ if .Get "role" }}
{{ $json = where $json (.Get "role") "in" "roles" }}
{{ end }}
<table>
{{ range $json }}
<tr>
<td>Name: {{ .name }}</td>
<td>Roles: {{ delimit .roles ", " }}</td>
</tr>
{{ end }}
</table>
And this is how I call the shortcode:
{{< people_list role="ga" >}}
Unexpectedly, the table is empty. How can I male where
select only those entries whose roles
array contains a certain value?