Root Element missing in json output

Root Element missing in json output

mcvrmcvr Posts: 4Questions: 2Answers: 0
edited July 2017 in Free community support

Hi,

I am unable to populate my dataTables due to the missing 'root' element.

Please see below code and help me how to fix it.

Piece 1:

  $('#example').DataTable({
       //"ajax": "density.txt",
       "ajax" : "getProductPropData",
       "dataType": 'json',
       "contentType": "application/json; charset=utf-8",
        "columns": [{
            "data": "densityId"
        }, {
            "data": "densityDescription"
        }, {
            data: null,
            className: "center",
            defaultContent: '<a href="#" id="edit" class="edit" data-toggle="modal" data-target="#myModal">Edit</a> / <a href="#" id="delete">Delete</a>'
                //defaultContent: '<button type="button" class="btn-default" data-toggle="modal" data-target="#myModal">Open Modal</button>'
        }]
    });

Piece 2: What I am in in my Struts 2 action class.

public String execute() throws Exception {

        SessionFactory sf = (SessionFactory) ctx.getAttribute("SessionFactory");
        ProductPropertyDAO pdao = new ProductPropertyDAOImpl(sf);
        List<DensityGroup> dg = pdao.getProductPropListData("Density");

        List<DensityGroup> list = new ArrayList<DensityGroup>();
        System.out.println("SIZE is: " + dg.size());

        for (int i = 0; i < dg.size(); i++) {
            DensityGroup denpojo = new DensityGroup();
            denpojo.setDensityId(dg.get(i).getDensityId());
            denpojo.setDensityDescription(dg.get(i).getDensityDescription());
            list.add(denpojo);
        }
        
        data = list;
        
        System.out.println("JSON is :\n" + data);
        return "success";
    }

My JSON Data from Chrome Network tab is:

[{"densityDescription":"16 KG","densityId":"21"},{"densityDescription":"Chitti","densityId":"22"}]

Correct data should be:

{"data" : [{"densityDescription":"16 KG","densityId":"21"},{"densityDescription":"Chitti","densityId":"22"}]}

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954
    edited July 2017

    Looks like you might need to use the ajax.dataSrc. Similar to the second example:
    https://datatables.net/reference/option/ajax.dataSrc#Examples

    You might not need the dataType and `contentType. But if you do then you need change the format from this:

    "ajax" : "getProductPropData",
    "dataType": 'json',
    "contentType": "application/json; charset=utf-8",
    

    Place all your ajax options in an object like this:

    "ajax" : {
      "url": "getProductPropData",
      "dataType": 'json',
      "contentType": "application/json; charset=utf-8",
      dataSrc: ''
    },
    

    Kevin

    edited by Allan - I've added the ajax.dataSrc option to the example for clarity.

  • mcvrmcvr Posts: 4Questions: 2Answers: 0

    Lovely, that solved my problem. Thanks a lot.

This discussion has been closed.