JSON String Format for Data Table

JSON String Format for Data Table

adevadeadevade Posts: 8Questions: 1Answers: 0

hello Iam using a vb.net service in which I am converting a Data view to JSON String, but the concern is that How will I convert the string into Data Table Compatible String Help me out with a solution.

Dim dvDataVw As New DataView(SendDataTable)
SendDataTable = dvDataVw.ToTable
json = JsonConvert.SerializeObject(SendDataTable, Formatting.Indented)

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,338Questions: 1Answers: 10,439 Site admin

    What JSON does the above result in? DataTables can read most JSON formats.

    Allan

  • adevadeadevade Posts: 8Questions: 1Answers: 0

    [
    {
    "dbName": "DEV12SEP",
    "cmpName": "BLANK12SEP",
    "versStr": "920180",
    "dbUser": "dbo",
    "LOC": "United States Of America/Puerto Rico"
    },
    {
    "dbName": "BLANKDBPL8",
    "cmpName": "BLANKDBPL8",
    "versStr": "920180",
    "dbUser": "dbo",
    "LOC": "United States Of America/Puerto Rico"
    },
    {.....}
    ]

  • allanallan Posts: 63,338Questions: 1Answers: 10,439 Site admin

    That looks good to me. See this example for how you can have DataTables read JSON in that form (i.e. a flat array of objects).

    Allan

  • adevadeadevade Posts: 8Questions: 1Answers: 0

    yes Allen it worked for me when I placed this hardcoded JSON into a txt File,but I am here throwing this same string from server side, what chnages I have to make to achieve that.

  • adevadeadevade Posts: 8Questions: 1Answers: 0

    <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
    [
    {
    "dbName": "DEV12SEP",
    "cmpName": "BLANK12SEP",
    "versStr": "920180",
    "dbUser": "dbo",
    "LOC": "United States Of America/Puerto Rico"
    },
    {
    "dbName": "BLANKDBPL8",
    "cmpName": "BLANKDBPL8",
    "versStr": "920180",
    "dbUser": "dbo",
    "LOC": "United States Of America/Puerto Rico"
    },
    {...}
    ]
    </string>

    //Because I am sending a variable which is of String type from server side, i.e by Serializind a DataTable into JSON

  • allanallan Posts: 63,338Questions: 1Answers: 10,439 Site admin

    Can you tell the framework not to encapsulate it in a string? I'm afraid that is outside my area of expertise. You'd need to ask of SO or similar.

    As it stands, the server isn't returning valid JSON which is why DataTables is rejecting it.

    Allan

  • adevadeadevade Posts: 8Questions: 1Answers: 0

    Dim dvDataVw As New DataView(SendDataTable) //SendDatatable contains the the Data Table in it

            SendDataTable = dvDataVw.ToTable
    
            json = JsonConvert.SerializeObject(SendDataTable, Formatting.Indented)
    

    //To convert the Data Table into the JSON format, the format here looks perfect but when this json returns to client end it gives problem i.e Data Table gives Invalid JSON error, but my JSON have a valid String form

  • kthorngrenkthorngren Posts: 21,246Questions: 26Answers: 4,929

    Have you tried pasting the returned JSON into https://jsonlint.com/ to verify its a valid format?

    Kevin

  • adevadeadevade Posts: 8Questions: 1Answers: 0
    edited October 2017

    Yes Kevin its a valid JSON format, and only creates problem when thrown to client end (i.w to DataTable) as it is (actually the string contains in a variable which is of
    String type and this variable is thrown to Client end directly after hitting the URL), You can see the JSON format attached as snaptinypic.com/r/fay5b8/9

  • kthorngrenkthorngren Posts: 21,246Questions: 26Answers: 4,929

    If I understand what Allan is saying this part:

    <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">

    Is causing an issue with Datatables. The rest of the format looks good. I think he is asking for the string encapsulation to be removed.

    Kevin

  • allanallan Posts: 63,338Questions: 1Answers: 10,439 Site admin
    Answer ✓

    Correct. The entire response from the server needs to be valid JSON - not just part of it. That XML tag is making it not valid JSON.

    Allan

  • adevadeadevade Posts: 8Questions: 1Answers: 0
    edited October 2017

    After some trails ,We achieved the Data putting into the table from Server side...
    Thanks for your support.

    $.ajax({
    type: "GET",
    url: [Service IP],
    crossDomain: true,
    dataType: "json",
    async: false,
    cache: false,
    success: function (data, status, xhr) {
    //console.log("From Server-->"+data);
    var objData = new Object();
    objData = JSON.parse(data);
    //console.log("--->parse"+objData);
    $('#example').DataTable({
    //"dataSrc": objData,
    data: objData,
    //"serverSide": true,
    // "processing": true,
    "columns": [
    { "data": "dbName" },
    { "data": "cmpName"},
    { "data": "versStr"},
    { "data": "dbUser"},
    { "data": "LOC"}
    ]
    });
    },
    error: function (xhr, status, error) {
    alert('Error Occured - ' + error);
    }
    });

  • allanallan Posts: 63,338Questions: 1Answers: 10,439 Site admin
    Answer ✓

    Good to hear - thanks for letting us know.

    Allan

This discussion has been closed.