Recently I was working on a project in which JSON was used, I had two approaches to get data from database to facilitate my JavaScript with Json string,

**First**

Is to do processing in database to form a Json string, like this 

```
select   
       ID,
    '{ "Comments": ['+
    Substring(
        (SELECT 
           ',{ '+
           '"Comment" : "' + REPLACE(Comments,'"','\"') + '",'+
           '"Name" : "' + REPLACE(Name,'"','\"') +
         '}'
    FROM    JsonTbl
    where   JsonTbl.ID = tbl.ID
    for xml path('')
    ),2,250000)
      +'] }' as JsonData
from tbl

```

**Second**

Is to select data from database without any processing and do processing of creating a Json string in front end, like this

```
DataTable data = GetDataFromDatabase();
StringBuilder sb = new StringBuilder();
sb.Append("{ \"Comments\": [");
for (int i = 0; i < data.Rows.Count; i++)
{
    sb.Append("{");
    sb.Append("\"Comment\" : \"" + data.Rows[i]["Comment"].ToString().Replace('"', '\"') + "\",");
    sb.Append("\"Name\" : \"" + data.Rows[i]["Comment"].ToString().Replace('"', '\"') + "\"");
    sb.Append("},");
}
sb.Remove(sb.Length - 1, 1);

```

I was wondering which one would be better from performance as well as maintenance point of view. 

NOTE : Please consider that I've trimmed the code to post here original code is much larger than this, and I'm using MSSQL 2008 and .Net(C#)