0

I have a JSON as below and I want to extract data from columns and rows array from it.

{
  "id":0,
  "tabName":"newtab",
  "columns":[
    {
      "id":0,
      "name":"firstname",
      "type":"entity",
      "operator":"=",
      "$$hashKey":"object:40"
    },
    {
      "id":1,
      "name":"address",
      "type":"domain",
      "operator":"<",
      "$$hashKey":"object:50"
    }
  ],
"rows":[
  {
    "0":"Adam", //this first row data corresponds to "Name" column
    "1":"5432 River Drive", //this first row data corresponds to "Address" column
    "$$hashKey":"object:34",
    "tabId":"1122", 
    "description":"New Tab",
    "id":1
  },
  {
    "0":"Silver",  //this 2nd row data corresponds to "Name" column
    "1":"Address 2", //this 2nd row data corresponds to "Address" column
    "$$hashKey":"object:53",
    "tabId":"2345",
    "description":"New 2nd Tab",
    "id":2
  }
],
"uniqueIdCounter":3
}

From the above JSON, I want to extract columns and rows values. This is just sample JSON and the actual one can have more column and rows entries.

In the columns array: id, name, type operator keys remain the same for every column entry.

In the rows array: tabid, description and id keys remain the same for every row entry.

The issue with the row array is the key values "0", "1" (that actually correspond to columns firstname & address). How to loop through this array and extract these dynamic values? By dynamic I mean if I have more than 2 rows, the keys in the rows would be "0", "1", "2" and so on for other rows.

I started with writing this loop:

var jsonColumns = JSON.parse(JSON.stringify($scope.myTable.columns));
for (var i = 0; i < jsonColumns.length; i++) {
    var obj = jsonColumns[i]; 
    //For columns data I can do obj.id, obj.name etc and works fine
}

For rows I am not sure how to proceed here:

var jsonRows = JSON.parse(JSON.stringify($scope.myTable.rows));
for (var i = 0; i < jsonRows.length; i++) {
    var obj = jsonRows[i]; 
    //How can I extract rows with keys "0", "1" where I dont know these key names in advance
}

Any inputs to this, please? Thanks

1
  • you can do Object.getOwnPropertyNames(jsonRows[i]) to get all the keys. Commented Jul 18, 2018 at 3:36

3 Answers 3

2

Assuming that the tabid, description, id and $$hashKey are static, you can extract those dynamic keys as follows,

let tableObj = {
  "id":0,
  "tabName":"newtab",
  "columns":[
    {
      "id":0,
      "name":"firstname",
      "type":"entity",
      "operator":"=",
      "$$hashKey":"object:40"
    },
    {
      "id":1,
      "name":"address",
      "type":"domain",
      "operator":"<",
      "$$hashKey":"object:50"
    }
  ],
"rows":[
  {
    "0":"Adam", //this first row data corresponds to "Name" column
    "1":"5432 River Drive", //this first row data corresponds to "Address" column
    "$$hashKey":"object:34",
    "tabId":"1122", 
    "description":"New Tab",
    "id":1
  },
  {
    "0":"Silver",  //this 2nd row data corresponds to "Name" column
    "1":"Address 2", //this 2nd row data corresponds to "Address" column
    "$$hashKey":"object:53",
    "tabId":"2345",
    "description":"New 2nd Tab",
    "id":2
  }
],
"uniqueIdCounter":3
};

let jsonRows = JSON.parse(JSON.stringify(tableObj.rows));
let staticAttributes = ['tabId', 'description', 'id', '$$hashKey'];
let extractedData = [];
for (const obj of jsonRows) {
    let extractedObj = {};
    
    for (let key in obj) {
      if (obj.hasOwnProperty(key) && !staticAttributes.includes(key)) {
        extractedObj[key] = obj[key];
      }
    }
    
    extractedData.push(extractedObj);
}

console.log(extractedData);

Sign up to request clarification or add additional context in comments.

Comments

0

You can extract key like this:

var jsonRows = JSON.parse(JSON.stringify($scope.myTable.rows));
for (var i = 0; i < jsonRows.length; i++) {
     var obj = jsonRows[i]; 
     console.log("Rows", obj["0"], obj["1"], obj["id"], obj["tabId"], obj["description"]); //here you can extract data with specified keys
}

Rest of your code will same.

Comments

0

Use for each loop for it :

var jsonData = {
    data: [{
        "key1": 1,
        "key2": 2
    }, {
        "key1": 3,
        "key2": 4
    }]
}
for (var index in jsonData.data) {
    for (var key in jsonData.data[index]) {
        console.log(key)
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.