title: How do I access all the nested data in the response from an API call and display it in a Jinja template?
body: <p>I’m building something with Python and Flask and Jinja templates that involves using data from an API call. Some of what comes back is nested inside objects (apologies if I get any terminology wrong, I’m quite new). For example, this is how I’m assigning what comes back in the response to instances of my Park model:</p>
  <pre><code>def show_parks():
      endpoint = &quot;https://developer.nps.gov/api/v1/parks?limit=50&amp;start=10&amp;API_KEY=apikey&quot;
      req = urllib.request.Request(endpoint)
  
      # Execute request and parse response
      response = urllib.request.urlopen(req).read()
      data = json.loads(response.decode('utf-8'))
      park_ids_in_db = [park.id for park in Park.query.all()]
  
      # Prepare and execute output
      for place in data[&quot;data&quot;]:
          if place[&quot;designation&quot;] == &quot;National Park&quot; and place[&quot;id&quot;] not in park_ids_in_db:
  
              park = Park(
                  id=place[&quot;id&quot;], 
                  name=place[&quot;fullName&quot;], 
                  code=place[&quot;parkCode&quot;], 
                  description=place[&quot;description&quot;], 
                  ent_fees_cost=place[&quot;entranceFees&quot;][0][&quot;cost&quot;], 
                  ent_fees_description=place[&quot;entranceFees&quot;][0][&quot;description&quot;], 
                  ent_fees_title=place[&quot;entranceFees&quot;][0][&quot;title&quot;], 
                  ent_passes_cost=place[&quot;entrancePasses&quot;][0][&quot;cost&quot;], 
                  ent_passes_description=place[&quot;entrancePasses&quot;][0][&quot;description&quot;], 
                  ent_passes_title=place[&quot;entrancePasses&quot;][0][&quot;title&quot;], 
                  # activity=activityNames,
                  activity=place[&quot;activities&quot;][0][&quot;name&quot;], 
               # data for activities in the JSON response looks like this: 
              &quot;activities&quot;: [
                  {
                      &quot;id&quot;: &quot;09DF0950-D319-4557-A57E-04CD2F63FF42&quot;,
                      &quot;name&quot;: &quot;Arts and Culture&quot;
                  },
                  {
                      &quot;id&quot;: &quot;FAED7F97-3474-4C21-AB42-FB0768A2F826&quot;,
                      &quot;name&quot;: &quot;Cultural Demonstrations&quot;
                  },
                  {
                      &quot;id&quot;: &quot;13A57703-BB1A-41A2-94B8-53B692EB7238&quot;,
                      &quot;name&quot;: &quot;Astronomy&quot;
                  },
                  {
                      &quot;id&quot;: &quot;D37A0003-8317-4F04-8FB0-4CF0A272E195&quot;,
                      &quot;name&quot;: &quot;Stargazing&quot;
                  },]
                  state=place[&quot;states&quot;], 
                  phone=place[&quot;contacts&quot;][&quot;phoneNumbers&quot;][0][&quot;phoneNumber&quot;], 
                  directions_url=place[&quot;directionsUrl&quot;], 
                  hours=place[&quot;operatingHours&quot;][0][&quot;description&quot;], 
                  town=place[&quot;addresses&quot;][0][&quot;city&quot;], 
                  image_title=place[&quot;images&quot;][0][&quot;title&quot;], 
                  image_altText=place[&quot;images&quot;][0][&quot;altText&quot;], 
                  image_url=place[&quot;images&quot;][0][&quot;url&quot;], 
                  weather_info=place[&quot;weatherInfo&quot;],)
  
              db.session.add(park)
  
      db.session.commit()
      return render_template('/logged_in_home.html', parks=Park.query.all())
  </code></pre>
  <p>Things like name and description just have one string in the response. But with something like activities, the response has an array with a bunch of objects for each activity. I want access to all those activities so I can list them all in a template. But if I do activity=place[“activities”][name”] without the [0] I get an error of ‘string indices must be integers.’ So I put that index of [0] and that gets around the error, but then I only have access to the first activity.</p>
  <p>My first stab at getting access to all of them while avoiding that error was to create an empty string and add the activities to it with a for loop, then pass that string into statement where instances of Park are created (taking all the other attributes out here just to make it easier to read):</p>
  <pre><code>for place in data[&quot;data&quot;]:
              
              activityNames = &quot;&quot;
              activities = place[&quot;activities&quot;]
              for activity in activities:
                  activityNames += activity
  
                  park = Park(
                      activity=activityNames,
  )
  </code></pre>
  <p>Then in my template I tried doing a for loop to go through the activities to display them:</p>
  <pre><code>Here are some activities you can do in the park:&lt;br&gt;
  {% for activity in activityNames %}
      {{ park.activity }}&lt;br&gt;
  {% endfor %}    
  </code></pre>
  <p>I don’t get an error when I load the template, but it ignores the activities, it just skips over them to the next thing. I can’t tell if I’m way off in my approach or on the right track. Can anyone give me a hand?</p>