how to properly construct script for nested json
how to properly construct script for nested json
aileenywdd
Posts: 3Questions: 1Answers: 0
i have a json that is structured like this:
{
"success": true,
"data": {
"events": {
"My Event Name": {
"date": "january 1, 2017",
"place": "My Home",
"participants": ["Merly", "Sonia"],
"ideas": {
"this is an idea": {
"idea_detail": {
"deets": ["1.00", "1.00"]
},
"last_update": 1517977880
},
"another idea": {
"idea_detail": {
"deets": ["1.02", "12.00"]
},
"last_update": 1517977882
}
},
"info": {
"display_name": "Display",
"num": 2,
"date": 1517977921
}
},
}
}
}
im not sure how i can construct my script so i could export these data into datatables, here is my test code
$('#my_table').DataTable( {
ajax: {
url: '/testapi.txt',
dataSrc: 'events'
},
columns: [
{ data: 'name' },
{ data: 'date' },
{ data: 'place' },
{ data: 'participants' },
{ data: 'ideas' },
{ data: 'ideas' },
{ data: 'info' }
]
} );
} );
But no data is being loaded.
Im new to json and any help would be much appreciated. thanks in advance
This discussion has been closed.
Answers
You'd use
data.events
sinceevents
is a nested object - however, DataTables expects an array of data - not an object of entries. You'd need to convert the object into an array.ajax.dataSrc
can be used as a function if you wanted to do that on the client-side.Allan
thank you for your reply,
i researched around and this is what i come up with
what could i be doing wrong?
im not sure how i could construct the script properly
i tried using the ajax.dataSrc base on what i could search online, but i believe im still lacking in knowledge with how to construct the scripts, could you help me?
It has no
length
property. It is an object, not an array. You'd need to usefor in
or$.map
or similar to convert it form being an object into an array.Allan