Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am trying to generate JSON from a query. All this seems to be too much grinding it out.

string function getJSON() output="false"    {   

  var result = "[";
  var myrow = 0;

  for(var row in this.qryLocation)  {
    myrow++;
    result &= "[" 
             & row.LocationID                     & ',' 
             & '"' & row.Address & " " & row.City & '",' 
             & '"' & row.formatted_CreateDate     & '"'
             & ']' 
             & myRow < this.qryLocation.recordcount ? ',' : ''; 
    } // end for-in query

  return result & "]";  
  }

All this seems very hard to read, especially making sure that the last row does not get an extra comma.

share|improve this question
    
That does seem quite clunky. I'm sure someone will have ideas on making this more readable and practical. – Phrancis Sep 2 at 2:17
    
Which version of ColdFusion? – Adam Cameron Sep 2 at 5:27
2  
It's difficult to answer well without knowing your version of CF, but as a practice: don't build a JSON string by hand: build a native data structure which represents the format you need, then serialise it to JSON when done. – Adam Cameron Sep 2 at 6:57
    
I am on ColdFusion 11 – James A Mohler Sep 2 at 15:33
    
The data comes from the result of a CFquery – James A Mohler Sep 2 at 15:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.