Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse a json dumped python dict as javascript object in django template

I have a python code which converts the normal dict into JSON as follows

groups = {
    'g_1': [
        {'name': 'something', 'id': '1'}, 
        {'name': 'something', 'id': '1'}
    ], 
    'g_2': [
        {'name': 'something', 'id': '1'}, 
        {'name': 'something', 'id': '1'}
    ]
}

I have used json.dumps to convert that to a json as follows

import json
groups = json.dumps(groups)

I want to read the data in javascript as an object. How to I access it?

I tried var groups=JSON.parse({{ groups }}) it didn't work out. Help me in parsing the data as javascript object.

like image 228
Ranjith Singhu Ganapathy Avatar asked Mar 08 '17 14:03

Ranjith Singhu Ganapathy


1 Answers

This should work:

var groups = JSON.parse('{{ groups|safe }}');

like image 160
nik_m Avatar answered Oct 05 '22 19:10

nik_m