A JavaScript function/object to easily work with ColdFusion's native query JSON format.
Grab it from github: Query.js.function Query(json) { /* A wrapper for CFML queries returned as JSON */ this.json = json; this.currentRow = -1; this.recordCount = this.json["DATA"].length; this.columnIndex = {}; for (var i = 0; i < this.json["COLUMNS"].length; i++) { // Map columns to an index for faster look ups this.columnIndex[this.json["COLUMNS"][i]] = i; } this.next = function() { // Progresses the current row return ++this.currentRow < this.recordCount; }; this.getValue = function(column) { // Return data for the given column based on the current row if (this.currentRow >= this.recordCount || this.currentRow < 0) { return null; } else { return this.json["DATA"][this.currentRow][this.columnIndex[column.toUpperCase()]]; } }; this.getValueAt = function(column, row) { // Dive into the data at a given row if (this.json["DATA"].length >= row) { return this.json["DATA"][row][this.columnIndex[column.toUpperCase()]]; } else { return null; } }; this.reset = function() { this.currentRow = -1; }; this.getCurrentRow = function() { return this.currentRow; }; this.getRecordCount = function() { return this.recordCount; }; };
next() boolean
Progresses the current row.
getValue(column) string/null
Return data for the given [column] based on the current row. Returns null if the pointer has progressed beyond the last row. Errors if the column doesn't exist.
getValueAt(column, row) string/null
Return data for the given [column] for the given row. Returns null if the pointer has progressed beyond the last row. Errors if the column doesn't exist.
reset()
Sets the pointer back to the start. The start is at the beginning of the query, not the first row so next() will need to be called before the first row is accessed.
getCurrentRow() numeric
Returns the current row.
getRecordCount() numeric
Returns the record count.
// Create a new clients query - Where json is a ColdFusion query JSON string var clients = new Query(json); // Loop over the clients and output their first name while (clients.next()) { console.log(clients.getValue("firstName")); }