0

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.

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for your answer. But console.log throws an error stating "SyntaxError: JSON.parse: unexpected character"
@Chuvi With this script ?
yup with this script only
it doesn't support nested Json, Include fields and then try.. its not working yar
2

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

Comments

0

use JSON:

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

7 Comments

thanks for your answer. But console.log throws an error stating "SyntaxError: JSON.parse: unexpected character"
@positlabs I suppose he is sending the "Json object" from django so he must use double braces to read the variable in javascript
@Chuvi If you are sending the "Json object" from django how are you sending it? are you using json.dumps with the correct mimetype?
@VictorCastilloTorres: you are correct im sending json object to script. see my edits for my full json object sent to script. by the way im She.
@VictorCastilloTorres: im using simplejson.dumps, mimetype is application/json
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.