[Answer]-Clean up Django Ajax JSON GET Request

1👍

Based on the comments you’ve left.. it seems your issue is downstream in the client (e.g. web browser). It is not clear what you mean by stuck in JSON. If you are using JavaScript to parse the JSON, you will need to use JSON.parse() to turn it into a native JavaScript object. If you are using jQuery and the $.ajax() method, you will need to set the mimetype to application/json for it to automatically parse it as JSON.

UPDATE

If you want to control how the JSON data is rendered in the browser, I suggest you parse the JSON response into a native JavaScript object and then iterate over objects and fields you want to render in the page. As an example, using jQuery:

$.ajax({
    url: '/some-url/',
    dataType: 'json',
    success: function(resp) {
        var i, k, li, obj, fields;

        for (i = 0; i < resp.length; i++) {
            obj = resp[i];
            // render obj pk or model name here... now iterate over fields
            fields = obj.fields;

            for (k of obj.fields) {
                li = $('<li>').text(k + ': ' + obj.fields[k]);
                // append the li to some element..
            }
        }
    }
});

Leave a comment