how to extract data from this array

how to extract data from this array

jonnyenglish89jonnyenglish89 Posts: 10Questions: 2Answers: 0

Hi, I'd like to extract the faction name from this array but the array’s structure is a little quirky

{
    "faction": {
        "faction_id": "148999003",
        "leader_id": "3800576",
        "name": "CarryTheNite",
        "power": "0",
        "num_members": "50",
        "inactive": "0",
        "sp_earned": "26510",
        "message": "VINI FUSES UP NEXT",
        "total_rating": 1032609,
        "recruitment_status": "1",
        "min_rating": "0",
        "min_level": "19",
        "total_level": "0",
        "vote_id": "3800576",
        "votes": 0,
        "max_members": 50,
        "guild_points": "26510",
        "faction_key": "4f0"
    }
}

I've tried this to no avail:

$(document).ready(function(){  
      $('#data-table').DataTable({  
           "ajax"     :     "source.json",  
           "columns"     :     [  
                {     "faction"     :     "name"     } 
           ]  
      });  
 });  

any help would be appreciated

Answers

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    did you try

    columns:[{data:"faction.name"}.......
    
  • jonnyenglish89jonnyenglish89 Posts: 10Questions: 2Answers: 0

    No, I hadn’t but when I did I got this error “DataTables warning: table id=data-table - Ajax error.”

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    did you define all of your columns that are being displayed (you need to do that if your data is in an array of json objects.

  • jonnyenglish89jonnyenglish89 Posts: 10Questions: 2Answers: 0
    <!DOCTYPE html>
    <html>
    <head>
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js">
        </script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js">
        </script>
        <link href="https://cdn.datatables.net/r/bs-3.3.5/jq-2.1.4,dt-1.10.8/datatables.min.css" rel="stylesheet" type="text/css">
        <script src="https://cdn.datatables.net/r/bs-3.3.5/jqc-1.11.3,dt-1.10.8/datatables.min.js" type="text/javascript">
        </script>
        <title></title>
    </head>
    <body>
        <br>
        <br>
        <div class="container">
            <h3 align="center">Test Table</h3><br>
            <table class="table table-bordered" id="data-table">
                <thead>
                    <tr>
                        <th>name</th>
                    </tr>
                </thead>
            </table>
        </div>
        <script>
         
        $(document).ready(function(){  
             $('#data-table').DataTable({
                  "ajax"     :     "source.json",  
                  "columns"     :     [  
                        {faction:"name"}
                       //{data: "faction.name"} 
                  ]  
             });  
        });  
        </script>
    </body>
    </html>
    
    

    Thanks for your help so far. I’ll continue to search the forum for an answer

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin

    {faction:"name"}

    There is no columns.faction property in DataTables, so that won't work.

    Can you upload source.json so we can see the exact structure of the file please?

    Allan

  • jonnyenglish89jonnyenglish89 Posts: 10Questions: 2Answers: 0

    Sure, I changed the ext from .json to .txt so i could upload it

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin

    In that JSON file, is there an array that you want to have displayed as the rows in the table? I see a lot of different objects, but there doesn't appear to be any suitable arrays.

    Allan

  • jonnyenglish89jonnyenglish89 Posts: 10Questions: 2Answers: 0

    Ultimately, I wanted to display:
    (conquest_data -> zone_data -> “name” + “my_guild_points”)
    into a table that I could filter and sort as required… it’s a shame it doesn’t seem possible with data tables as they would have been perfect

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin

    my_guild_points appears to be an integer value:

    "my_guild_points": "5066",

    What is the per row data point here? For example load this page and click the "Ajax" tab below the table. You will see an array of objects. Each array entry is a row in the table.

    What do you want to show per row?

    Allan

  • jonnyenglish89jonnyenglish89 Posts: 10Questions: 2Answers: 0

    this is what I'd like to be displayed in the data table

    <!DOCTYPE html>
    <html>
    <head>
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js">
        </script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js">
        </script>
        <link href="https://cdn.datatables.net/r/bs-3.3.5/jq-2.1.4,dt-1.10.8/datatables.min.css" rel="stylesheet" type="text/css">
        <script src="https://cdn.datatables.net/r/bs-3.3.5/jqc-1.11.3,dt-1.10.8/datatables.min.js" type="text/javascript">
        </script>
        <title></title>
    </head>
    <body>
        <br>
        <br>
        <div class="container">
            <h3 align="center">Test Table</h3><br>
            <table class="table table-bordered" id="data-table">
                <thead>
                    <tr>
                        <th>name</th>
                        <th>my_guild_points</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>Red Maw Base</td>
                        <td>5066</td>
                    </tr>
                    <tr>
                        <td>Magma Foundry</td>
                        <td>3681</td>
                    </tr>
                    <tr>
                        <td>Asphodel Nexus</td>
                        <td>7125</td>
                    </tr>
                    <tr>
                        <td>Baron's Claw Labs</td>
                        <td>2017</td>
                    </tr>
                    <tr>
                        <td>Borean Forges</td>
                        <td>2487</td>
                    </tr>
                    <tr>
                        <td>Phobos Station</td>
                        <td>1677</td>
                    </tr>
                    <tr>
                        <td>Elder Port</td>
                        <td>1042</td>
                    </tr>
                    <tr>
                        <td>Jotun's Pantheon</td>
                        <td>1530</td>
                    </tr>
                    <tr>
                        <td>Mech Graveyard</td>
                        <td>782</td>
                    </tr>
                    <tr>
                        <td>Malort's Den</td>
                        <td>494</td>
                    </tr>
                    <tr>
                        <td>Colonial Relay</td>
                        <td>1795</td>
                    </tr>
                    <tr>
                        <td>Enclave Landing</td>
                        <td>1167</td>
                    </tr>
                </tbody>
            </table>
        </div>
        <script>
            
           $(document).ready(function(){  
                $('#data-table').DataTable();  
           });
        </script>
    </body>
    </html>
    

    Hope that helps

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin

    You'd need to modify the JSON structure (either on the server-side or the client-side, it doesn't really matter) before DataTables sees it. DataTables needs an array of items, where each item is a row to be shown - the item can be a complex object, but fundamentally it must be an array.

    Allan

  • jonnyenglish89jonnyenglish89 Posts: 10Questions: 2Answers: 0

    Ok, thanks for your help!

This discussion has been closed.