0

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.

3
  • @RajaSimon I tried, <p>Anger - {{ data.emotion.document.template.anger }}</p> and it returned nothing but 'Anger - '. Any other thoughts. Thanks. Commented Jun 4, 2017 at 17:34
  • Yes. It returns, Anger - {u'document': {u'emotion': {u'anger': 0.088841, u'joy': 0.048228, u'sadness': 0.115575, u'fear': 0.078872, u'disgust': 0.33166}}} Commented Jun 4, 2017 at 17:45
  • @RajaSimon, oops! I my typo. I built up the call layer by layer after your suggestion and realized I included the typo 'template' instead of 'emotion' in the third layer. Works now. If you want to enter your assistance as a solution rather than a comment I'll mark it as the solution. Thanks. Commented Jun 4, 2017 at 17:52

1 Answer 1

2

You can access the dict of dict with . so the solution to your question is data.emotion.document.emotion.anger

Sign up to request clarification or add additional context in comments.

1 Comment

With the JSON structured as it is in the original post the precise syntax would be data.emotion.document.emotion.anger but the solution absolutely solved the problem. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.