Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datatables throwing "invalid JSON" error when loading from local file

I'm trying to pull the following JSON data from a local file into DataTables, but getting an invalid JSON response:

    {
    "data": [
        {
            "accountNumber": "2423",
            "domain": "domain.com",
            "playerClass": "",
            "adTag": ""
        },
        {
            "level": "info",
            "message": "generator ",
            "timestamp": "2015-06-10T15:59:02.803Z"
        }
    ]
}

Using:

    $(document).ready(function () {
        $('#content').dataTable({
            "ajax": 'test.log'
        });
    });

JSFIDDLE

like image 789
Matt Avatar asked Jun 23 '26 15:06

Matt


1 Answers

It's because you do in fact have invalid JSON. When using datatables, according to the documentation, your data source always needs to be an array: https://www.datatables.net/manual/data

Here is what it should look like:

{
    "data": [
        {
            "accountNumber": "1234",
            "domain": "domain.com",
            "playerClass": "Player",
            "adTag": ""
        },
        {
            "accountNumber": "1234",
            "domain": "domain.com",
            "playerClass": "Player",
            "adTag": ""
        }
    ],
    "level": "info",
    "message": "tag generator ",
    "timestamp": "2015-06-09T21:00:45.776Z"
}

When you create JSON, you should always validate it to make sure it is valid - check out http://jsonlint.org

like image 188
Jack Avatar answered Jun 26 '26 12:06

Jack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!