how to define the key in object and use it to define its objects in datatabke column

how to define the key in object and use it to define its objects in datatabke column

icdeadpeopleicdeadpeople Posts: 3Questions: 0Answers: 0

my json data(products.json) is like this how do i define my table columns

      {
                "users" : 
                {
                  "sdsdsds234adasadasdasdasdasd" : {"Money" : 150,"diamonds" : 13,"level" : 8},
                  "sdsdsds234adasadrrtyyu442dcd" : {"Money" : 200,"diamonds" : 0,"level" : 9},
                  "sadasdasdasd324324sddsf35w4t" : {"Money" : 800,"diamonds" : 1,"level" : 1},
                  "0ytrvdgksksydklelleıudfhfewn" : {"Money" : 477,"diamonds" : 4,"level" : 9}
                }
        }

example data table
$('#data-table').DataTable({
data :response.products.users,
[
{ "data":'sdsdsds234adasadasdasdasdasd' (which is userid) },
{ "data":'userid.Money' },
{ "data":'userid.diamonds' },
{ "data":'userid.level' }
]

Replies

  • kthorngrenkthorngren Posts: 21,150Questions: 26Answers: 4,919
    edited July 2019

    You will need to structure your data in a format Datatables supports. This doc explains the supported data structures:
    https://datatables.net/manual/data/#Data-source-types

    Essentially you will need an array of objects. You userid would also need to be an object. Your data would need to look more like this:

    {
        "users": [{
                "userid": "sdsdsds234adasadasdasdasdasd",
                "Money": 150,
                "diamonds": 13,
                "level": 8
            },
            {
                "userid": "sdsdsds234adasadrrtyyu442dcd",
                "Money": 200,
                "diamonds": 0,
                "level": 9
            },
            {
                "userid": "sadasdasdasd324324sddsf35w4t",
                "Money": 800,
                "diamonds": 1,
                "level": 1
            },
            {
                "userid": "0ytrvdgksksydklelleıudfhfewn",
                "Money": 477,
                "diamonds": 4,
                "level": 9
            }
        ]
    }
    

    Then your Datatable like this:

    $('#data-table').DataTable({
    data: response.products.users,
      [
        { "data":'userid' (which is userid) },
        { "data":'Money' },
        { "data":'diamonds' },
        { "data":'level' }
      ]
    });
    

    This example shows how to access nested objects:
    https://datatables.net/examples/ajax/deep.html

    Kevin

  • icdeadpeopleicdeadpeople Posts: 3Questions: 0Answers: 0

    Thank you for the reply Kevin,
    If you are saying it is not possible I can do nothing while data base contains 10k from this object and it will take too much time to edit add replace brackets braces.
    Actually with the code that I find(below) I can get correct result without changing the
    "sdsdsds234adasadasdasdasdasd" part as value of userid

     $('#data-table').DataTable({
                            data :response.products.users,
                            "columns": [{
                                "data": function ( row) {
                                  return Object.keys(row)[0];
                                },
                            }, {
                                "data": function ( row) {
                                  return Object.keys(row)[0].Money;
                                },
                            }, {
                                "data": function ( row) {
                                  return Object.keys(row)[0].diamonds;
                                },},
                              {
                                "data": function ( row) {
                                  return Object.keys(row)[0].level;
                                },
                            }], 
    

    But as I said I need the way if it is possible to get result without changing json data

  • icdeadpeopleicdeadpeople Posts: 3Questions: 0Answers: 0

    I found the answer in the forum Thank you for the reply.
    the answer I found is in the link.
    https://www.datatables.net/forums/discussion/56567/how-to-get-data-from-json-with-tree-format-2

This discussion has been closed.