DATA: Range through json file and use value to access another json file

I am trying to access a json file using a key extracted from another json file. Anybody to put me on track?

sample file 1: test_species.json

[
{
    "name": "Autour des Palombes",
    "species": "oiseau",
    "scientific": "Accipiter gentilis",
    "protections": ["EU","BE"]
},
{
    "name": "Accenteur mouchet",
    "species": "oiseau",
    "scientific": "Prunella modularis",
    "protections": []
},
{
    "name": "Balbusard Pêcheur",
    "species": "oiseau",
    "scientific": "Pandion haliaetus",
    "protections": ["EU","ES", "DE"]
}
]

sample file 2: test_protection_countries.json

{
    "BE": {"country": "Belgique"},
    "FR": {"country": "France"},
    "ES": {"country": "Espagne"},
    "PT": {"country": "Portugal"}

}

shortcode file

{{ $birds := .Site.Data.test_species }}
{{ $countries := .Site.Data.test_protection_countries }}

<div class="list_birds">
    <ul>
        {{ range $birds }}
            {{ if eq .species "oiseau" }}
                <li id="list_birds">
                {{ .name }} (<i>{{ .scientific }}</i>)
                {{ if gt (len .protections) 0 }} Prot: 
                    {{ range $key, $value :=.protections }}
                        <!-- Pseudo code: $countries[$value] -->
                    {{ end }}
                {{ end }}
            </li>
            {{ end  }}
        {{ end }}
    </ul>
</div>

See the index func.

1 Like
{{ range $key, $value :=.protections }}
    {{ if isset $countries $value }}
        {{ index (index $countries $value) "country"}}
    {{ end }}
{{ end }}

Thanks, that did the trick.

Nice SSG by the way. Amazingly fast. Kudos to all the dev’s.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.