[Fixed]-Access Field Values from JSON serialized django object

1👍

Accessing the serialized django object

To access the serialized django object you need to first parse the data variable like this:

var obj = JSON.parse(data);

And then you can access the fields of each object Individually like this:

// for the first object
obj[0]['fields']['id']            // or  obj[0].fields.id
obj[0]['fields']['group_name']   // or  obj[0].fields.group_name

// for the second object
obj[1]['fields']['id']            // or  obj[1].fields.id
obj[1]['fields']['group_name']   // or  obj[1].fields.group_name

// and so on...

In your case you can do this:

$.each(obj, function(index){ // for each object
    $(groups).append('<option value=' + obj[index]['fields']['id'] + '>'+ obj[index]['fields']['group_name'] +  '</option>');                                                      
});   

Leave a comment