JSON formatting error

JSON formatting error

firblazerfirblazer Posts: 2Questions: 0Answers: 0
edited April 2011 in General
Hi!

I am getting this error:

DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error.

I have:

{
"iTotalDisplayRecords":10,
"iTotalRecords":3,
"aaData":[{"1","null","0","192.168.1.182","stop"},{"2","null","0","192.168.1.182","stop"},{"3","null","0","192.168.1.182","stop"}]
}


For my table, I have 5 columns. Please help. Thanks a lot.

Replies

  • allanallan Posts: 63,516Questions: 1Answers: 10,473 Site admin
    Just as the error says, your string isn't valid JSON ( http://jsonlint.com ). Rather than returning an array of objects (which aren't actually objects), DataTables currently requires an array of arrays.

    Allan
  • GregPGregP Posts: 500Questions: 10Answers: 0
    edited April 2011
    Something about the JSON generated by a PHP + SQL solution is infrequently invalid for reasons way beyond my understanding. A timing issue on the server side maybe... unexpected characters in the record... I don't know. But suffice it to say that it's worth intercepting the JSON somehow before it gets written back to the requesting client. Maybe output it as a textfile from PHP before sending it along.

    The advice stands even if you're not using PHP; I just mention it because I had the error periodically when polling SQL through PHP; since switching to a Java Server application, we are not seeing the same JSON parse errors anymore.

    It'll just tell you what you already know: that the JSON is invalid; but seeing it on the server instead of on the client side is often the concrete validation that puts you into "debug the server side" mode instead of "debug DataTables configuration" mode.

    Greg
  • firblazerfirblazer Posts: 2Questions: 0Answers: 0
    Hi!

    I have

    [code]
    {
    "sEcho": 1,
    "iTotalDisplayRecords": 1,
    "iTotalRecords": 1,
    "aaData": [
    [
    "1",
    "null",
    "0",
    "192.168.1.182",
    "stop"
    ]
    ]
    }
    [/code]

    but I am getting the formatting error. It has passed JSONLint.
  • allanallan Posts: 63,516Questions: 1Answers: 10,473 Site admin
    That does indeed look like valid JSON, but if you are still getting the error, then there must be something else being sent from the server (a debug echo or something?) causing it to be invalid. If you could give us a link that would be useful.

    Allan
This discussion has been closed.