Syntax error when parsing Json data in javascript

advertisements

I am returning this in my views.py through a dictionary

{"injured_json": [{"pk": 24, "model": "appvisual.injured_count", "fields": {"Y_2010": 75445, "Y_2008": 70251, "Y_2009": 70504, "Y_2004": 57283, "Y_2005": 62006, "Y_2006": 64342, "Y_2007": 71099, "State_UT": "Tamil Nadu", "Y_2003": 55242, "Y_2011": 74245}}], "total_json": [{"pk": 23, "model": "appvisual.total_accident", "fields": {"Y_2010": 64996, "Y_2008": 60409, "Y_2009": 60794, "Y_2004": 52508, "Y_2005": 53866, "Y_2006": 55145, "Y_2007": 59140, "State_UT": "Tamil Nadu", "Y_2003": 51025, "Y_2011": 65873}}], "killed_json": [{"pk": 24, "model": "appvisual.killed_count", "fields": {"Y_2010": 75445, "Y_2008": 70251, "Y_2009": 70504, "Y_2004": 57283, "Y_2005": 62006, "Y_2006": 64342, "Y_2007": 71099, "State_UT": "Tamil Nadu", "Y_2003": 55242, "Y_2011": 74245}}, {"pk": 60, "model": "appvisual.killed_count", "fields": {"Y_2010": 15409, "Y_2008": 12784, "Y_2009": 13746, "Y_2004": 9507, "Y_2005": 9758, "Y_2006": 11009, "Y_2007": 12036, "State_UT": "Tamil Nadu", "Y_2003": 9275, "Y_2011": 15422}}]}

while retrieving the about json in javascript, the json data gets enclosed with ( and ) as follows :

({injured_json:[{pk:24, model:"appvisual.injured_count", fields:{Y_2010:75445, Y_2008:70251, Y_2009:70504, Y_2004:57283, Y_2005:62006, Y_2006:64342, Y_2007:71099, State_UT:"Tamil Nadu", Y_2003:55242, Y_2011:74245}}], total_json:[{pk:23, model:"appvisual.total_accident", fields:{Y_2010:64996, Y_2008:60409, Y_2009:60794, Y_2004:52508, Y_2005:53866, Y_2006:55145, Y_2007:59140, State_UT:"Tamil Nadu", Y_2003:51025, Y_2011:65873}}], killed_json:[{pk:24, model:"appvisual.killed_count", fields:{Y_2010:75445, Y_2008:70251, Y_2009:70504, Y_2004:57283, Y_2005:62006, Y_2006:64342, Y_2007:71099, State_UT:"Tamil Nadu", Y_2003:55242, Y_2011:74245}}, {pk:60, model:"appvisual.killed_count", fields:{Y_2010:15409, Y_2008:12784, Y_2009:13746, Y_2004:9507, Y_2005:9758, Y_2006:11009, Y_2007:12036, State_UT:"Tamil Nadu", Y_2003:9275, Y_2011:15422}}]})

Because of additionally added "(" and ")" i could not parse the json dta in javascript. How can i eliminate this syntax error.

My Views.py

def get_details(request):
    import pdb;pdb.set_trace();
    total_details = total_accident.objects.filter(State_UT='Tamil Nadu')
    total_details = serializers.serialize('python', total_details)
    killed_details = Killed_Count.objects.filter(State_UT='Tamil Nadu')
    killed_details = serializers.serialize('python', killed_details)
    injured_details = Injured_Count.objects.filter(State_UT='Tamil Nadu')
    injured_details = serializers.serialize('python', injured_details)
    page_data = {
    "total_json" : total_details,
    "killed_json" : killed_details,
    "injured_json" : injured_details,
    }
    page_data= simplejson.dumps(page_data)
    print page_data
    return render_to_response('dvslzer.html', {'page_data':page_data})

My Script:

function test() {

    var dataRows = {{page_data}};
    console.log(dataRows.toSource());
    var data=JSON.parse(dataRows.total_accident); // throws syntax error
    console.log(data[0].pk);
};

Is there any solution to get rid of this syntax error??


Piecing together what we managed to establish in comments above, this line:

var data=JSON.parse(dataRows.total_accident);

...should actually be:

var data = dataRows.total_json;

Because firstly there is no property in the object called total_accident, and secondly it doesn't make sense to try to use JSON.parse() because you're not actually dealing with JSON at that point.

(The JS isn't really dealing with JSON at all, because the server-side {{page_data}} outputs the JSON directly into the page source, so by the time the browser sees it it just appears as an object literal in your JS code. If it was JSON you'd need to use JSON.parse() on dataRows before you could start accessing properties with dot notation.)