Chuvi
Chuvi

Reputation: 1298

Trouble with Parsing JSON in Javascript

I generated the following json in my views.py

page_data= {'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}}]'}

I passed this json to my javascript. My question is how can i manipulate over this json in my javascript.

I tried the following code but it doesn't work:

function test() {
    var dataRows = {{page_data}};
    console.log(dataRows.injured_json[0].pk); };  

Edit : My Full json object to the javascript

{'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}}]'}

NOTE:

I found that the json data i sent is not a valid json. i rectified it. but now the prolem is, i am sending the json data in dictionary. while receiving the json data in javascrpit,the format becomes ( json data ). because of extra added " (" and ")" , i could not parse the json in javascript. how can i eliminate "(" and ")" to parse the json data in javascript.

Upvotes: 0

Views: 94

Answers (3)

Victor Castillo Torres
Victor Castillo Torres

Reputation: 10811

use JSON:

function test() {
    var data = "{{page_data}}";
    var dataRows = JSON.parse(data.injured_json);
    console.log(dataRows[0].pk); };

Upvotes: 0

posit labs
posit labs

Reputation: 9471

I'm not sure why everyone is pointing to JSON.parse. The issue here is that you're attempting to access an array, but it's actually a string. Simply remove the quotes from around the array.

Actually... You don't need an array at all. I think you are looking for something more like this:

{
    "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
        }
    }
}

The problem was just with the json formatting.

console.log(page_data.injured_json.pk); // logs 24

Upvotes: 2

user2226755
user2226755

Reputation: 13223

See JSON.parse() :

var page_data= {'injured_json': '[{"pk": 24, "model": "appvisual.injured_count"}]'},
    dataRows = JSON.parse(page_data.injured_json);

console.log(dataRows[0].pk);

Upvotes: 2

Related Questions