0

I have scraped json data from other web and want to out put to web, but in the backend of flask, it only return a list, just like: [{'aa':'bb', 'cc':'dd'}]. How do I turn it to be json format so js script on html able to get these json data from backend?

app.py:

url1 = requests.get('https://xxxxxxx/getJSON.aspx?jsontype=xxxx.aspx')
json_data = json.loads(url1.content.decode())
json_data = json.dumps(json_data)
json_data = json.loads(json_data)
return render_template('index.html', json_data=json_data)

html script:

<script>
const json_org = JSON.parse({{ json_data }});
    const json_data = json_org
    ;

    const output = document.getElementById('output');

    output.innerHTML = json_data;
</script>

Error:

Uncaught SyntaxError: Unexpected token '&'

const json_org = JSON.parse([{&#56;name&#56;: &#56;Current&#56;},...}])
Hugo Yu
  • 105
  • 9

1 Answers1

0

Add safe filter, this will help,

JSON.parse('{{ json_data |tojson | safe}}');
NavaneethaKrishnan
  • 1,213
  • 1
  • 9
  • 22