0

I am using requests within python and getting a string output in the form: {"a":1,"b":2,"c":3,"d":4,"e":5}.

I am then using flask to return the output and trying to turn it into a table within my html template.

some_output = response.text
return render_template('the-template.html', some_output=some_output)

printing some_output gives : {"a":1,"b":2,"c":3,"d":4,"e":5}

within the template i am trying:

<html>
   <body>
      <table>
         {% for key, value in some_output.items() %}
            <tr>
               <th> {{ key }} </th>
               <td> {{ value }} </td>
            </tr>
         {% endfor %}
      </table>
   </body>
</html>

When i submit the page to get the output i get the following error message:

jinja2.exceptions.UndefinedError: 'str object' has no attribute 'items'

How can i convert this string object into a dictionary so i can turn the dictionary into an html table within the template??

Bash----
  • 65
  • 3

1 Answers1

2

You can use json.loads to convert the string to a dictionary object:

import json
some_output = response.text
return render_template('the-template.html', some_output=json.loads(some_output))
Ajax1234
  • 58,711
  • 7
  • 46
  • 83