Search code examples
jsonnunjuckseleventy

Show specific JSON values in a nunjuck template


Trying filter values from a JSON-file and showing in an nunjuck-template. This is the JSON-file:

{"rows":
 [
   {"value":
    {
     "name":"'s Gravenwezel",
     "zip":"2970"
    }
   },
   {
    "value":
    {
     "name":"'s Herenelderen",
     "zip":"3700"
    }
   },
   ...
  }
 ]
}

When adding {{ cities }} in the nunjuck-template, it shows the whole JSON-file, but how can i show only the specific values name and zip?

Trying this:

{{ cities.rows }}

{{ cities.rows[0] }}

{{ cities.rows[0].value[0].name }}

...

Also trying with a for-loop:
{% for city in cities %}
  {{ city[0] }}
{% endfor %}

And a lot of other combinations. Nothing works!


Solution

  • First, I'd modify the for loop to use cities.rows. Then you can address keys via value.X. So for example:

    {% for city in cities.rows %}
     {{ city.value.name }} and {{ city.value.zip }}<br/>
    {% endfor %}