0

I have json in this format:

[
    {"aaa":"aaa"},
    {"Columns":[
        [{"bbb":"bbbb"}, {"name":"name1"}, {"gender":"gender1"}],
        [{"bbb":"bbbb"}, {"name":"name2"}, {"gender":"gender2"}],
    ]}
]

I tried:

$(document).ready(function() {
    $('#example').DataTable( {
        "ajax": "data/file.txt",
        "columns": [
            { "data": "Columns.0.name" },
            { "data": "Columns.0.gender" },
        ]
    } );
} );

but it gave me an error: Uncaught TypeError: Cannot read property 'length' of undefined How should I read name and gender? Thanks.

1 Answer 1

1

Use the code below:

var table = $('#example').DataTable({
    ajax: {
       url: 'https://api.myjson.com/bins/2p254',
       dataSrc: '1.Columns'
    },
    columns: [
        { "data": '0.name' },
        { "data": '0.gender' }
    ]        
});

See this jsFiddle for code and demonstration.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that works, but I got the json wrong in the post. I have updated my post. Could you please have a look. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.