I am trying to print the results of an API call which is returning JSON results nested relatively deeply. I am doing this project with Python 2.7 and Django 1.11.
I have the following view.py function:
def nlu_analysis(request):
if request.method == 'POST':
text2send = request.POST.get('text2send')
natural_language_understanding = NaturalLanguageUnderstandingV1(
version='2017-02-27',
username='####',
password='####')
response = natural_language_understanding.analyze(
text=text2send,
features=[features.Entities(), ..., features.SemanticRoles()])
return render(request, 'watson_nlu/analysis.html', {'data': response})
When I use the following template code in my .html file:
{% for k in data.keywords %}
<p>Text - {{ k.text }}</p>
<p>Relevance - {{ k.relevance }}</p>
{% endfor %}
to parse and display JSON with one level of nesting like this:
'keywords': [{
'relevance': 0.946673,
'text': 'eyes'
}]
Everything is great and it displays 'eyes' and 0.946673 as expected.
I can't figure out the appropriate syntax for getting to the 'anger', 'joy', etc. results that are nested more deeply such as this:
{
'emotion': {
'document': {
'emotion': {
'anger': 0.195192,
'joy': 0.082313,
'sadness': 0.644314,
'fear': 0.207166,
'disgust': 0.103676
}
}
}
What is the most efficient method for accomplishing this objective?
It is definitely NOT:
<p>Anger - {{ data['emotion.document.template.anger'] }}</p>
Advance newbie gratitude and good juju for your help.