Find exact string in a list of strings

Hi!

I have a .json data file with the following content:

{
  "groups": [
    {
      "name": "shape.blue",
      "description": "Medium size blue shapes"
    },
    {
      "name": "shape.blue.large",
      "description": "Large blue shapes"
    },
    {
      "name": "shape.blue.small"
      "description": "Small blue shapes"
    }
    {
      "name": "shape.green",
      "description": "Medium size green shapes"
    },
    {
      "name": "shape.green.large",
      "description": "Large green shapes"
    },
    {
      "name": "shape.green.small"
      "description": "Small green shapes"
    }
],
“shapes” : [
    {
      "name": "shape.blue.circle",
      "description": "Medium-size blue circle"
    },
    {
      "name": "shape.blue.large.square"
      "description": "Large blue square"
    },
    {
      "name": "shape.blue.small.circle"
      "description": "Small blue circle"
    }
]
)

I would like to create a layout where I generate subheadings from the names in “groups”. Under each subheading, there would be a table listing the shapes where the shape name contains the group name. It would look like this:

### shape.blue

| Name | Description |
|-------|--------|
| shape.blue.circle | Medium-size blue circle                |

### shape.blue.large

| Name | Description |
|-------|--------|
| shape.blue.large.square  | Large blue square                 |

etc.

Here’s my layout so far:

{{ define "main" }}
   <h1>{{.Title}}</h1>

   {{ .Content }}

   <h2>Shapes</h2>

   {{ $data := index .Site.Data.shapes .Params.datafile "shapes" }}
   {{ range $data.groups }}
   <h3>{{ .name }}</h3>
   {{ $group := .name }}
   <table>
    <thead>
    <tr>
    <th>Name</th>
    <th>Description</th>
    </tr>
    </thead>
    <tbody>
    {{ range $data.shapes }}
    {{ if in .name $group }}
    <tr>
    <td>{{ .name }}</td>
    <td>{{.description}}</td>
    </tr>
    {{ end }}
    {{ end }}
    </tbody>
    </table>
    {{ end }}
    {{ end }}

My issue is that this way, all the instances of a group name get listed. That is, the list for shape.blue would contain shape.blue.large.square and shape.blue.small.circle, because they contain shape.blue. But I’d like these to only be listed in their respective sections, shape.blue.large and shape.blue.small

I experimented with regex, because I suspect that’s the way to go, but I couldn’t come up with a way to do it. I’d appreciate some suggestions on how to handle this.

Unfortunately I can’t restructure the data file, so I have to work with this.

Thank you very much!

The data structure is terrible, but you might look at the where function’s like operator.

I looked at where with like but couldn’t get it to work so far. Can you use a variable ($group in my example) as part of a regex?

{{ $re := `^shape\.green\.` }}
{{ $d := where site.Data.foo.groups "name" "like" $re }}