Newbie Question. How do I get this format ot display?

Newbie Question. How do I get this format ot display?

PWHPWH Posts: 26Questions: 5Answers: 0

I have been trying for a week to log into my API with username and password via ajax/GET etc without any success. However, I have managed to download and save the information with curl and save it to a text file. The format is (from their site)

{"success":true,"balances":{"BTC":"13.00419483","XMR":"396.93688709","LTC":"2.00000000","SUMO":"1.00000000","ETN":"0.10000000","AEON":"0.00000000","XVG":"1.00000000","BCN":"2.20000000","FBF":"1.00000000","XAO":"2.00000000","ITNS":"20.00000000"}}

I have managed to get the file to load and I can see in console.log that the info has loaded, but it will not load to the table. The error is

XML Parsing Error: not well-formed
Location: file:///F:/Test/working/balances.txt
Line Number 1, Column 1: balances.txt:1:1
balances: Object { BTC: "0.00000000", XMR: "0.00000000", LTC: "0.00000000", … }

So, I guess it doesn't like that format? The code I have is

$(document).ready( function () {
var table = $('#balances').DataTable({
  columns: [
    { data: 'success' },
    { data: 'balances'}
   
  ]
});
            
    $.getJSON("balances.txt", function(data) {
                console.log(data)
                var dt_data = [];
      
                for (var i=0; i<data.length; i++) {

                  var trade_name = Object.keys(data[i])[0];
                  console.log("trade Name" + trade_name)
                  var trades = data[i][trade_name];
                  
                  trades.name = trade_name;
                  dt_data.push(trades);

                }  
                console.log(dt_data)
                table.rows.add( dt_data ).draw();
            });
  
} );

I have tried with the codeyou gave me for the other table removed with the same result.
How can I get the data to display?
Thank you

Answers

  • colincolin Posts: 15,146Questions: 1Answers: 2,586

    This is your data here:

    { 
       "success":true,
       "balances":{ 
          "BTC":"13.00419483",
          "XMR":"396.93688709",
          "LTC":"2.00000000",
          "SUMO":"1.00000000",
          "ETN":"0.10000000",
          "AEON":"0.00000000",
          "XVG":"1.00000000",
          "BCN":"2.20000000",
          "FBF":"1.00000000",
          "XAO":"2.00000000",
          "ITNS":"20.00000000"
       }
    }
    

    If you want a two-column table, you would need the data to look like:

    { 
       "success":true,
       "balances":{ 
          ["BTC","13.00419483"],
          [XMR","396.93688709"],
    ...
    

    See example here. You would need to also set ajax.dataSrc to be balances,

    Colin

  • PWHPWH Posts: 26Questions: 5Answers: 0

    Thank you Colin for the reply
    I am obviously missing something fundametntal as it just won't work for me. The example works, but not with my file. My new code, after trying many variants is

    $('#example').dataTable( {
      
      "ajax": {
        "url": "balances.txt",      // Load the file
        "dataSrc": function (balances) {
          for ( var i=0, ien=balances.length ; i<ien ; i++ ) { // find the length
          }
          console.log(balances); // show it in the console
          return balances;
        }
      }
    } );
    

    but I get the same error(s)

    This page uses the non standard property “zoom”. Consider using calc() in the relevant property values, or using “transform” along with “transform-origin: 0 0”. balances.html
    XML Parsing Error: not well-formed
    Location: file:///F:/Test/working/balances.txt?_=1580085955837
    Line Number 1, Column 1: balances.txt:1:1
    
    balances: {…}
    ​​
    ACM: "0.00000000"
    ​​
    AEON: "0.0000000"
    ​​
    ARQ: "0.00000000"
    ​​
    ARRR: "0.00000000"
    ​​
    BBS: "0.00000000"
    

    etc,etc. So the file is loading, The datatable says "no data available in table"
    What am I missing??
    Thank you

  • PWHPWH Posts: 26Questions: 5Answers: 0

    I would add that the example data files all have [ ] square brackets in them but my data file does not. Is that an indication of what needs doing?

  • PWHPWH Posts: 26Questions: 5Answers: 0

    After much Googling and trial & error, I have the correct result displayed. It needed to have the data converted to an array to include the [ ] In case it helps anyone the code is

    $(document).ready(function() {
     var table = $('#example').dataTable( {
       result: [
                { balances: 'BTC' },
                ]
            ,
       
      "ajax": {
        "url": "balances.txt",      // Load the file
        "dataSrc": function (data) {
          
          var result = [];
          for(var i in data.balances)
                result.push([i, data.balances [i]]);
                  
          console.log(result); // show it in the console
          return result;
        }
      }
    } );
    } );
    

    However, I still get the error in the console log

    This page uses the non standard property “zoom”. Consider using calc() in the relevant property values, or using “transform” along with “transform-origin: 0 0”. balances.html
    XML Parsing Error: not well-formed
    Location: file:///F:/Test/working/balances.txt?_=1580100249043
    Line Number 1, Column 1: balances.txt:1:1
    

    I would like to know how to clear it in case it causes problems down the line..
    Thanks

  • colincolin Posts: 15,146Questions: 1Answers: 2,586

    Glad working. The error isn't from DataTables I'm afraid, so I can't help you with that,

    Colin

  • PWHPWH Posts: 26Questions: 5Answers: 0

    Thank you for your help it is appreciated

This discussion has been closed.