Use range and where in JSON data files

I have this JSON data file - see example:

       [
	{
		"date": "2019-4-22_16-21",
		"site": "https://www.bnbc.com/",
		"link": "https://www.bnbc.com/sites/"
	},
	{
		"date": "2019-4-22_16-21",
		"site": "https://www.wdc.com/new/",
		"link": "https://www.wdc.com/new-sites/"
	},
	{
		"date": "2019-4-23_18-22",
		"site": "https://www.bnbc.com/new/",
		"link": "https://www.bnbc.com/new-sites/"
	}
]
  1. I try to go first to find the items with a certain date (date = 2019-4-22_16-21)
    {{ $json := getJSON "data/snippets.json" }}

    {{ $mydate := (where $json "date" .Params.date) }}

which is fine

  1. Then I go to check the link to have in it “bnbc”
    {{ $bnbc := (where $mydate "link" "in" "bnbc") }}

On the second step unfortunately the range is empty, can you tell me why?

From the docs on where: https://gohugo.io/functions/where/

Syntax

where COLLECTION KEY [OPERATOR] MATCH

in

true if a given field value is included in a matching value; a matching value must be an array or a slice

So your snippet is testing if the link field value is contained in "bnbc". What I think you want is the reverse: test if "bnbc" is contained in the value of the link field.

I don’t think you can do that inside a where.

{{ $bnbc := slice }}
{{ range $mydate }}
  {{ if in .link "bnbc" }}
    {{ $bnbc = $bnbc | append . }}
  {{ end }}
{{ end }}

Thank you … that works now