user2997109 user2997109 - 1 year ago 84
JSON Question

Getting data from JSON Structure

My coldfusion component is returning a JSON format data. I am trying to access the values of that data in my front-end using Javascript. Can someone help me understand how to access the data values such as the "id", "firs_name" and "last_name"?

I am storing the the follow data in the variable called tempData. Below is the JSON structure I am getting:

{ "COLUMNS" : [ "id",
"DATA" : [ [ "xxxx",
[ "xxx2",

Below is my ajax call:

type: "get",
url: "GROUPSLIST.cfc",
data: {
method: "getNames",
queryString: selectQuery
success: function(a) {
error: function(a) {

Answer Source

I am assuming your response is not parsed already. You can use something like following.

var tempData = '{"COLUMNS":["id","FIRST_NAME","LAST_NAME"],"DATA":[["xxxx","Jes","Abr"],["xxx2","JESSIE","YU"]]}';
//parse response if not already parsed 
var respObj = JSON.parse(tempData);
var columns = respObj['COLUMNS'];
//create a column map to index like following
var colMap = {};
for(i = 0; i < columns.length; i++){
  colMap[columns[i]] = i;
var data = respObj['DATA'];
var text = ''
//use data[i][colMap['id']] to access data inside loop.
for(i = 0; i < data.length; i++){
  text += data[i][colMap['id']] + ':' +data[i][colMap['FIRST_NAME']] + ' ' + data[i][colMap['LAST_NAME']] + '<br>';  
document.getElementById('text').innerHTML = text;
<div id="text"></div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download