Kevin
Kevin

Reputation: 86

Converting TSQL to JSON

Update 3-12-13: I have fixed this problem by using a normal SQL query and pasting the right brackets and quotes between the data. When I copy and paste this result to my JSON file the list is updated.

Thank you for your help all.

I have a problem with parsing data I get from my database to a JSON output. I've never used TSQL but it seems like the best way to do it. The desired JSON output is this:

{
     "persons": {
     "person": [
     {
    "name": "Kevin Wtenweerde",
    "phone": "231",
    "mobile": "0612345678"
     }
    ]
  }
}

My database table is called list and has the fields name, phone and mobile.

Does anyone have any suggestions on how to do this?

Thanks in advance everyone.

Upvotes: 2

Views: 883

Answers (1)

Remus Rusanu
Remus Rusanu

Reputation: 294297

T-SQL is not the appropriate language to to do this job. Use T-SQL (as well as any other SQL flavor) to manipulate sets and to retrieve results from the database. A primer SQL is beyond the point here. One you have the desired result in your process, use your favorite JSON library to create the JSON and then serve it to whatever your consumer is.

Yes, there will be mavericks that will show some TSQL code that creates JSON. Heck, I've seen TSQL that creates raw PDFs. Just because is possible, doesn't mean you should. There are factors to consider like maintenability, readability, where are the back-end CPU cycles best spent, etc etc.

Upvotes: 3

Related Questions