how to properly construct script for nested json

how to properly construct script for nested json

aileenywddaileenywdd 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

Answers

  • allanallan Posts: 63,208Questions: 1Answers: 10,415 Site admin

    You'd use data.events since events 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

  • aileenywddaileenywdd Posts: 3Questions: 1Answers: 0
    edited February 2018

    thank you for your reply,
    i researched around and this is what i come up with

    $('#example').DataTable({
      //'deferRender': false,
      'ajax'       : {
        "type"   : "POST",
        "url"    : "/test.txt",
        "dataSrc": function (json) {
          var return_data = new Array();
          for(var i=0;i< json.data.events.length; i++){
            return_data.push({
              'event': json.data.events[i],
              'date': json.data.events[i].date,
              'place': json.data.events[i].place,
              'participants': json.data.events[i].participants,
              'ideas': json.data.events[i].ideas,
              'detail': json.data.events[i].ideas[i].idea_detail[i],
              'last_update': json.data.events[i].ideas.last_update,
              'info': json.data.events[i].info
            })
          }
          return return_data;
        }
        
        },
        "columns"    : [
        {'data': 'event'},    
        {'data': 'date'},
        {'data': 'place'},
        {'data': 'participants'},
        {'data': 'ideas'},
        {'data': 'detail'},
        {'data': 'last_update'},
        {'data': 'info'}
      ] 
    }) 
    
    

    what could i be doing wrong?
    im not sure how i could construct the script properly

  • aileenywddaileenywdd Posts: 3Questions: 1Answers: 0
    $('#example').DataTable({
      //'deferRender': false,
      'ajax'       : {
        "type"   : "POST",
        "url"    : "/test.txt",
        "dataSrc": function (json) {
          var return_data = new Array();
          for(var i=0;i< json.data.events.length; i++){
            return_data.push({
              'event': json.data.events[i],
              'date': json.data.events[i].date,
              'place': json.data.events[i].place,
              'participants': json.data.events[i].participants,
              'ideas': json.data.events[i].ideas,
              'detail': json.data.events[i].ideas[i].idea_detail[i],
              'last_update': json.data.events[i].ideas.last_update,
              'info': json.data.events[i].info
            })
          }
          return return_data;
        }
        
        },
        "columns"    : [
        {'data': 'event'},    
        {'data': 'date'},
        {'data': 'place'},
        {'data': 'participants'},
        {'data': 'ideas'},
        {'data': 'detail'},
        {'data': 'last_update'},
        {'data': 'info'}
      ] 
    }) 
    

    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?

  • allanallan Posts: 63,208Questions: 1Answers: 10,415 Site admin

    json.data.events.length

    It has no length property. It is an object, not an array. You'd need to use for in or $.map or similar to convert it form being an object into an array.

    Allan

This discussion has been closed.