Getting Data From Correct JSON Object?

Getting Data From Correct JSON Object?

DanJ210DanJ210 Posts: 23Questions: 5Answers: 0
edited November 2017 in Free community support

I'm using the code below that I worked out with another member to display data within datatables. It works great when I was give the JSON data under it.

var workerDataTable = $('#wTable').DataTable({
            "ajax": {
                url: "/Home/returnWorkerJson",
                dataSrc: function (data) { return JSON.parse(data); }
            },
            columns: [
                { data: "Attributes.Client" },
                { data: "Attributes.ver" },
                {
                    data: "LastCommunication",
                    render: function (data, type, row)
                    {
                        //var newTime = data.slice(0, 19);
                        var newDate = new Date(data);
                        return newDate.toLocaleDateString() + ' ' + newDate.toLocaleTimeString();
                    }
                },
                { data: "TelephonyStatistics.BY" },
                { data: "TelephonyStatistics.DA" },
                { data: "TelephonyStatistics.DL" },
                { data: "TelephonyStatistics.FX" },
                { data: "TelephonyStatistics.NR" },
                { data: "TelephonyStatistics.OI" },
                { data: "TelephonyStatistics.TO" },
                { data: "TelephonyStatistics.WA" },
                { data: "TelephonyStatistics.WL" },
                { data: "TelephonyStatistics.UNK" },
                { data: "WorkItemsGiven" },
                { data: "WorkItemsReturned" }
            ]
        });
[
    {
        "HordeJobId":"3c5a575e-7117-4717-b5d9-551cb689b2e3",
        "JobStatus":1,
        "JobType":"CodeRED_Next.Horde.CRNSMSJob",
        "Name":"CRN1306050 SMS - QuickText_636459312666437753 - ClientID: 2557 ClientName: Beltrami County, MN OrgID: 2557 OrgName: Beltrami County, MN UserID: 6130",
        "PerfCounters":
        {
            "databaseSelectBuffer":0.0,
            "totalRecipients":1.0,
            "unprocessedResults":0.0,
            "injectedWorkItems":0.0,
            "resultsWriteback":0.0,
            "dbSelectTaskRunning":0.0,
            "resultProcessorInjectorTaskRunning":1.0,
            "resultWritebackTaskRunning":1.0,
            "naturalCompletionTaskRunning":1.0,
            "dbTotal":1.0
        },
        "TotalWorkItems":1,
        "WorkItemsGiven":1,
        "WorkItemsReturned":0
    }
]

The above works but I need the bigger part which is the entire object and its JSON is a little different. Like this. I've only included a snippet of the entire JSON being returned but notice the curly braces instead of regular braces, like the above JSON has. I'm also only trying to access the "ServerName" property so once I figure that out I can deal with the rest.

{
            "Counters":[],
            "ServerName":"All",
            "JobCount":0,
            "WorkerCount":10,
            "Active":true,
            "AcceptingJobs":true,
            "EstimatedCapacity":0,
            "JobsInfo":[],
            "DialerThresholds":null,
}
var workerDataTable = $('#wTable').DataTable({
            "ajax": {
                url: "/Home/returnWorkerJson",
                dataSrc: function (data) { return JSON.parse(data); }
            },
            columns: [
                { data: "ServerName" }
            ]
        });

But when I try to use the same datatable code above and have the column display "ServerName", I get data loading but never loading anything. Does anyone know what I'm missing? I see this in the examples and I'm trying the same way in the examples but whether I have a dataSrc or not, nothing loads. Whether I try to parse it into a JSON object like the above, nothing loads. I even tried added regular braces to the beginning and end, like the first JSON dataset has but still nothing loads.

Any help is much appreciated. I can show the full JSON being returned, but I thought the very beginning was enough to understand what kind of JSON object is in play here.

This question has an accepted answers - jump to answer

Answers

  • DanJ210DanJ210 Posts: 23Questions: 5Answers: 0

    It's gotta be in the JSON object being returned. Let me format the entire JSON out and post that before anyone spends any thought on this.

  • allanallan Posts: 63,464Questions: 1Answers: 10,466 Site admin

    Yup - if you could show us was /Home/returnWorkerJson returns, exactly, that would be really useful.

    Thanks,
    Allan

  • DanJ210DanJ210 Posts: 23Questions: 5Answers: 0
    edited November 2017

    Ok so I dumbed down everything and created my own JSON to return. Here is what's being returned.

    {
        "serverName":"s2-vhorde2",
        "lastCommunication":null,
        "Attributes":
        {
            "state":"USA",
            "Client":"XDial",
            "ver":"10.14.1.13"
        },
        "TelephonyStatistics":null,
        "Client":"XDial",
        "BY":null,
        "DA":null,
        "DL":null,
        "FX":null,
        "NR":null,
        "OI":null,
        "TO":null,
        "UNK":"0",
        "WA":null
    }
    

    Here is the DataTable code

    var workerDataTable = $('#wTable').DataTable({
                ajax : "/Home/returnWorkerJson",
                columns: [
                    { data: "serverName" }
                ]
            });
    

    Table just says loading. I've tried many combinations including this one

    var workerDataTable = $('#wTable').DataTable({
                "ajax": {
                    url: "/Home/returnWorkerJson",
                    dataSrc: ""
                },
                columns: [
                    { data: "serverName" }
                ]
            });
    

    This one tells me that paramater name "serverName" is not found. But it clearly is up there. So what am I missing?

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947
    Answer ✓

    I believe Datatables is expecting an array of objects, even if only one, as described here:
    https://datatables.net/manual/data/#Objects

    I think your JSON needs to look like this:

    [
    {
        "serverName":"s2-vhorde2",
        "lastCommunication":null,
    ......
        "UNK":"0",
        "WA":null
    }
    ]
    

    Kevin

  • DanJ210DanJ210 Posts: 23Questions: 5Answers: 0

    What's confusing is in the examples, the link I'll give below. In the column data point section there's object data being used which is exactly what I'm trying to use. Check it out. https://datatables.net/manual/ajax

    I'll try adding braces to the front and end of the json but that wouldn't explain what I'm not understanding about what's shown in the link.

  • DanJ210DanJ210 Posts: 23Questions: 5Answers: 0

    After adding the brackets and then using JSON.parse(data) on the data being returned to dataSrc... The name is being displayed. But I don't know what I'm missing where I have to add brackets but in one of the examples it shows that I shouldn't. So I'm doing something wrong.

    I suppose this can work though. From a professional viewpoint, idk if my fellow coworkers would think it looks too pretty but if it works then oh well.

  • allanallan Posts: 63,464Questions: 1Answers: 10,466 Site admin

    Fair point - I'll tidy up the documentation there. The problem with having just a single object like that, is that you'd only be able to display a single row! You need an array of objects.

    Allan

  • DanJ210DanJ210 Posts: 23Questions: 5Answers: 0

    Ok sounds good. Thanks, Kevin and Allan. I'll look into a way to convert my object into an array that adds the brackets as a byproduct instead of inserting them into a string.

    I appreciate the help from both of you. Thank you.

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    I don't know what I'm missing where I have to add brackets

    The brackets represent an array of data. If you were to return more than one row of objects then the objects would need to be in an array. In this case you are returning one and it still needs to be in an array.

    Kevin

This discussion has been closed.