Need help filtering JSON with front matter param

I have the following JSON:

[{
    "id": 1,
    "title": "",
    "description": "",
    "price": 30.00,
    "brand": "",
    "images": [{
      "sort": 1,
      "alt": "product",
      "image": "hoodie-man-1.png"
    }],
    "options": [{
      "sizes": "S,M,L,XL,XXL",
      "colors": "Black,Purple,Charcoal,Grey"
    }],
    "tags": "hoodie,mens",
    "categories": ["Mens"],
    "collection": "boomers",
    "ribbon": {
      "enabled": true,
      "class": "primary",
      "text": "New"
    },
    "enabled": true
  },
  {
    "id": "2",
    "title": "",
    "description": "",
    "price": 30.00,
    "brand": "",
    "images": [{
      "sort": 1,
      "alt": "product",
      "image": "hoodie-man-2.png"
    }],
    "options": [{
      "sizes": "S,M,L,XL,XXL",
      "colors": "Black,Purple,Charcoal,Grey"
    }],
    "tags": "hoodie,mens",
    "categories": ["Mens"],
    "collection": "boomers",
    "ribbon": {
      "enabled": true,
      "class": "primary",
      "text": "New"
    },
    "enabled": true
  }

I’m trying to filter this with the following (in a partial):

{{ range where (getJSON "/data/products.json") "id" .params.product_id }}
{{ $product := . }}
{{ end }}

undefined variable “$product” because there is no match in the data file.
.params.product_id outputs as 1.

Any ideas? Any help is appreciated.

@malpaso
I think I have a solution for you but won’t be able to get to it for another 30 min until I get home. To save time though, any chance you have a link to source I can run locally?

So, without a bit more context, I think the following is what you’re trying to get. It uses the index function:

{{ $products := getJSON "/data/products.json" }}
{{ $productid := .Params.id }}
{{ $thisproduct := index $products $productid }}
<p>{{ $thisproduct }}</p>

There are ways to make this terser, I’m sure, but hopefully it illustrates the use of index. LMK if that doesn’t work and we can try something else.

Hi @rdwatters
Thanks for replying. Unfortunately your solution doesn’t work.

(edit) The below does not work, please ignore!

I have been able to come up with a solution that works, perhaps there is a more abbreviated way to do it though :wink:

{{ $product_id := .params.product_id }}
{{ $catalog := getJSON "/data/products.json" }}
{{ $scr := newScratch }}
{{ $scr.Set "product" slice }}
{{ range where $catalog "id" $product_id }}
{{ $scr.Add "product" (slice .) }}
{{ end }}
{{ $product := $scr.Get "product" }}
<p>{{ $product }}</p>

So I’m back to square one. The following code does not ‘find’ the product within products.json

{{ range where $catalog "id" $product_id }}
{{ $scr.Add "product" (slice .) }}
{{ end }}

The method that does work is having to loop over the entire products.json file like so:

{{ range $catalog }}
{{ if eq $product_id .id }}
... product stuff ...
{{ end }}
{{ end }}

Problem solved.

{{ $product_id := .params.product_id }}
{{ $catalog := getJSON "/data/products.json" }}
{{ $scr := newScratch }}
{{ $scr.Set "product" slice }}
{{ range where $catalog "id" $product_id }}
{{ $scr.Add "product" (slice .) }}
{{ end }}
{{ $product := $scr.Get "product" }}

{{ range $product }}
{{ .id }}
{{ .title }}
...etc...
{{ end }}

I had to change the "id’ in products.json to a string and make sure the front matter had a string identifier as well for ‘product_id’

1 Like