'Unable to return json values retrieved from MongoDB to HTML page in python flask

I'm able to retrieve required value from MongoDB individually and also able to print same values but unable to return all those values to HTML page. When I try to return those values I'm able to see only first value though I have used for loop in jinja.
My code is as follows:

@app.route('/webhookdisplay', methods=['POST', 'GET'])
def webhooksdis():
    collection10 = db['webhooks']
    a = collection10.find({"name": "abc"}, {'_id': 0, 'recorded_at':0, 'expiry_time': 0, 'version': 0, 'created_at': 0, 'account_id': 0, 'device_id': 0})
    for i in collection10.find({}):
        d = i.get('data', {}).get('geometry', {}).get('coordinates')
        print(d)
        name = i.get('data', {}).get('geofence_metadata', {}).get('name')
        print(name)
    return render_template("webhooks.html",  name = name, a = a, d =d)

In the above code name has None values also, so when I try to return it individually shows TypeError: 'NoneType' object is not iterable. Suppose if I return those values which dowsn't have None values it gets returned but displays only first value.

HTML code

{% for i in d %}
{{ i }} 
{% endfor %}<br />

This jinja works for all values but I require for individual values which I'm retrieving in flask:

<table>   
<th>           
{% for item in a %} 
</th>
<tr>
<td><th>   {% for key, value in item.items() %} </th>  </td>
<td><span>{{ key }} : {{ value }}</span> </td>
<br />
<td>{% endfor %}</td>
<td>{% endfor %}</td>
</tr>   
</table>


**Expected Output** All values including None values should be returned which I'm retrieving in flask so that I can render in HTML page


Solution 1:[1]

You can use jinja for template rendering.

Using below pseudo code you can populate JSON data:

{% for key, value in a %}        
  <span>{{key}} : {{value}}</span>
{% endfor %}

Solution 2:[2]

What is the output of:

print(request.data)
print(request.form)
print(request.json)
print(request.get_json())  

?

UPDATE:

a is a list. So try:

{% for item in a %} 
    {% for key, value in item.items() %}        
        <span>{{ key }} : {{ value }}</span><br />
    {% endfor %}
{% endfor %}

Solution 3:[3]

if a:   
    filtered_data=[{"name":device["name"]} for device in a]  
    print(filtered_data) 
else:   
    print("Document does not exist !")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 NiravHR
Solution 2
Solution 3 Anar Salimkhanov