I would like to pull specific values from two types of JSON files. After reading all of the posts on the subject and trying index, map, etc. I haven’t found a way to get this data out of the JSON files.
Any help would be much appreciated. Thanks y’all!
Example 1: In an array, how can I get the value for a key based on another key/value pair?
For example: rating key for “product_id”: “77777” key
data/reviews.json (from Stamped.io)
[
{
"product_id": "77777",
"rating": 4.8,
},
{
"product_id": "88888",
"rating": 3.4,
}
]
Example 2: How can I get the key in a nested array?
For example price key for a variant id under a given product_id (in other words how can I get the price of the “Hugo Hula Hoop”?)
data/productlist.json (from Shopify)
{
"products": [
{
"id": 1111,
"title": "Hugo Bobblehead",
"variants": [
{
"id": 5555555,
"product_id": 1111,
"price": "1000",
}
],
{
"id": 2222,
"title": "Hugo Hula Hoop",
"variants": [
{
"id": 6666666,
"product_id": 2222,
"price": "25",
}
],
}
]
}
What I’ve tried
With help from other posts in the community I found it easy to range through parts of the JSON.
Such as
Range to show the values of one key:
{{ range $.Site.Data.reviews }}
{{ index . “productId” }}
{{ end }}
Range to show the values of multiple keys:
{{ range $index, $element := $.Site.Data.productlist.products }}
(product) id:{{ $element.id }}
(product) title:{{ $element.title }}
Fwiw, the documentation on index function and on data templates both show examples where the data files use path to access values. And this user, @kaicastledine, had a similar question which they ended up using file paths to access values.