Parse data with no String name

I am building a new connector and some of the JSON data has no string names i.e.

{"user1":["PC1"],"user2":["PC4","PC5"],"user3":["PC1","PC2"]…..

If I use magicparse I just get 2 (very long) rows, where I'm after a column of names and a column of where they are seen.

is it possible to reference the data in another way such as

for(var i = 0; i < data.length; i++){

datagrid.addCell(data[i].[1])

datagrid.addCell(data[i].[2])

…..

Tagged:

Best Answer

  • ggenovese
    ggenovese Coach
    Answer ✓

    Hi @SimonKing_Atolls

    You'll need two loops, one for each user and one for each pc:

    jsonData = {"user1":["PC1"],"user2":["PC4","PC5"],"user3":["PC1","PC2"]}
    
    datagrid.addColumn('User',datagrid.DATA_TYPE_STRING)
    datagrid.addColumn('PC',datagrid.DATA_TYPE_STRING)
    
    for (var user in jsonData) {
        if (jsonData.hasOwnProperty(user)) {
            DOMO.log("User: " + user);
            var pcs = jsonData[user];
            for (var i = 0; i < pcs.length; i++) {
                DOMO.log("  PC: " + pcs[i]);
                datagrid.addCell(user);
                datagrid.addCell(pcs[i]);
                datagrid.endRow();
            }
        }
    }
    

Answers

  • ggenovese
    ggenovese Coach
    Answer ✓

    Hi @SimonKing_Atolls

    You'll need two loops, one for each user and one for each pc:

    jsonData = {"user1":["PC1"],"user2":["PC4","PC5"],"user3":["PC1","PC2"]}
    
    datagrid.addColumn('User',datagrid.DATA_TYPE_STRING)
    datagrid.addColumn('PC',datagrid.DATA_TYPE_STRING)
    
    for (var user in jsonData) {
        if (jsonData.hasOwnProperty(user)) {
            DOMO.log("User: " + user);
            var pcs = jsonData[user];
            for (var i = 0; i < pcs.length; i++) {
                DOMO.log("  PC: " + pcs[i]);
                datagrid.addCell(user);
                datagrid.addCell(pcs[i]);
                datagrid.endRow();
            }
        }
    }
    
  • This is brilliant, many thanks :)