How to add data with unique keys via JSON/AJAX?

How to add data with unique keys via JSON/AJAX?

davmidavmi Posts: 1Questions: 1Answers: 0

Hi guys!

I'd appreciate any insight here. I have a datatable which uses JSON data and pulls via AJAX.

The goal is to utilize data from arrays with unique key values. Unfortunately, I only get [object Object] as the response.

What would be the best way to pull such data?

Here is my code:
Javascript:

$(function () { $('#example1').DataTable({ "ajax": { "url": "db/data.json", }, "columns": [ { "data": { "CVS": "name" }}, { "data": { "Costco": "name" }} ], 'paging' : true, 'lengthChange': false, 'searching' : false, 'ordering' : true, 'info' : true, 'autoWidth' : false }) })

HTML:

Name

JSON:

{
"data":[
{
"CVS":[
{
"name":"lisinopril"
},
{
"name":"adderall"
}
]
},
{
"Costco":[
{
"name":"ibuprofen"
},
{
"name":"methylphenidate"
}
]
}
]
}

Ultimately, each primary category (CVS, Costco, etc) will have multiple keys, such as 'dosage', but I simplified the code to make it easier to understand what I'm looking to do.

Admittedly, I'm only just now learning the basics of Javascript, let alone libraries/frameworks like datatables, so I sincerely apologize for my lack of understanding. Again, any help/input is highly appreciated.

Thanks,

-Dave

Answers

  • allanallan Posts: 62,211Questions: 1Answers: 10,205 Site admin

    This doesn't actually mean anything to DataTables for the columns.data option.

    { "data": { "CVS": "name" } }

    In fact it is that which explains why you are seeing Object object since that data value is being converted to be a string.

    Moreover, DataTables will take data from a single array for the rows. Each entry in that array must be a row. It cannot read column information from multiple arrays as you appear to be doing above.

    More information about this is available here.

    Allan

This discussion has been closed.