how to get data from json with tree format

how to get data from json with tree format

ycusoyycusoy Posts: 27Questions: 5Answers: 0
edited May 2019 in Free community support

hi admin. i using example from datatables with json like:
{"id":"id_1", "name":"name_1", "job":"job_1", "address":"address_1" }
and its successfull and smart.

how about insert data from json with this format :smile:
{"id1":{ "name":"name_1", "job":"job_1", "address":"address_1" }, "id2":{ "name":"name_2", "job":"job_2", "address":"address_2" }, ... }

thanks.

example from script like this>>
<script> $(document).ready(function() { $('#currs').DataTable({ "ajax": { "url": "https://www.zpool.ca/api/currencies", "dataSrc": "", }, "columns": [{ "data": "algo" }, { "data": "port" }, { "data": "name" }, { "data": "height" }, { "data": "workers" }, { "data": "shares" }, { "data": "hashrate" }] }); }); </script>

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 15,186Questions: 1Answers: 2,590
    Answer ✓

    Hi @ycusoy ,

    That's not possible, see the examples like this one - you can't have unique object names for each row. If there's no way to send the correct data, you could preparse and recreate the data on the client in the expected format.

    Cheers,

    Colin

  • ycusoyycusoy Posts: 27Questions: 5Answers: 0

    @colin , is there no way that you can place the first row in one column then define the results of the data row in the first row, and continue on the second row onwards. like the repetition function to read json data. B) B) B)

  • kthorngrenkthorngren Posts: 20,458Questions: 26Answers: 4,803
    edited May 2019 Answer ✓

    Datatables expects the row data to be an array of objects or an array of arrays, as described here:
    https://datatables.net/manual/data/#Data-source-types

    Each array element is considered a row in Datatables. Even though it is a valid JSON string the JSON you have is not a supported data structure for Datatables:

    {
        "id1": {
            "name": "name_1",
            "job": "job_1",
            "address": "address_1"
        },
        "id2": {
            "name": "name_2",
            "job": "job_2",
            "address": "address_2"
        }
    }
    

    You would need to convert the data structure to a supported structure. You can either do this in the server script or you could use ajax.dataSrc as a function to process the returned JSON into a supported structure. See the 3rd example in the docs.

    Kevin

  • ycusoyycusoy Posts: 27Questions: 5Answers: 0

    @kthorngren, no idea and solution, I'm stuck in error.

    $('#example').dataTable( {
      "ajax": {
        "url": "data.json",
        "dataSrc": function ( json ) {
          for ( var i=0, ien=json.data.length ; i<ien ; i++ ) {
            ?????? ;
          }
          return json.data;
        }
      }
    } );
    
  • kthorngrenkthorngren Posts: 20,458Questions: 26Answers: 4,803
    edited May 2019

    First its a matter of learning how to iterate through your objects. Here is one of many tutorials:
    https://zellwk.com/blog/looping-through-js-objects/

    I chose to use the first example here:
    http://live.datatables.net/kumixemu/1/edit

    you could use something similar then return the generated array (data) instead of json.data. There are probably better, more efficient ways to do this. Will let you decide if you need to do it differently based on performance.

    Kevin

This discussion has been closed.