Get JSON from SQL Server

On Thomas Frank's blog there is an interesting post which shows how to get JSON from MySql, and I though about doing the same to SQL Server since I use it more than any other database lately. So what I did was create a stored procedure which uses cursor to format the string with our data. I'm not really good with SQL, so I managed to use two, and the procedure itself isn't really what you would call a masterpiece. Sorry about that. But what I want to show here is just a concept tho.

Here's the Procedure.

When I execute it like this:

exec getJson 'announcement', 4

I'll get a single column named 'json' with *all* my data, in this case, 4 registries from a table named announcement. Something similar to this:

[{'Id':'1','Title':'<h3>1. Lorem ipsum dolor sit a','Date':'Jun 16 2008  5:44PM','Text':'<p>Lorem ipsum dolor sit amet,'},{'Id':'2','Title':'<h3>2. Quisque aliquet accumsa','Date':'Jun 16 2008  5:44PM','Text':'<p>Lorem ipsum dolor sit amet,'},{'Id':'3','Title':'<h3>3. Duis consequat laoreet<','Date':'Jun 16 2008  5:44PM','Text':'<ul><li>Duis consequat laoreet'},{'Id':'4','Title':'<h3>4. Aenean consectetuer</h3','Date':'Jun 16 2008  5:44PM','Text':'<p>Quisque aliquet accumsan le'}]

Then if you're using one JavaScript widget to slide news/announcements, it might be useful. Just pass this returning text to the eval method and you have a fresh json object, ready to use.

NOTE: it shouldn't be used with large amounts of data since you're populating a varchar sql variable, and it has a limit. If you try to pass more data than it can handle, you'll get errors.

Published Monday, November 17, 2008 2:01 PM by tsantos
Filed under: ,

Comments

# Enlaces JSON y .NET

Monday, March 02, 2009 9:27 AM by Enlaces JSON y .NET

Pingback from  Enlaces JSON  y .NET

# re: Get JSON from SQL Server

Thursday, November 05, 2009 12:54 PM by hector

Hi..

Im tryng to get son data from SQL and show them in a google API but i read i need a JSON to make this posible,,, thats true? can u help me?

# re: Get JSON from SQL Server

Sunday, February 07, 2010 6:04 AM by Achutha Krishnan BR

     This article is good. Normally what I do is, get the datatable in code-behind and loop the datatable and convert it into json string and perform my operations. This method seems to be good. As you've used cursor, I have a question. Suppose we've 10000+ records, what would be the execution cost of it? Will this approach be better than converting in c# code-behind?

# re: Get JSON from SQL Server

Sunday, June 13, 2010 3:43 AM by !S!WCRTESTINPUT000001!E!

!S!WCRTESTTEXTAREA000003!E!

# re: Get JSON from SQL Server

Wednesday, June 23, 2010 12:26 PM by Harry

IN case your interested in a way to do go the other way..

www.sql-library.com

# re: Get JSON from SQL Server

Saturday, July 03, 2010 4:44 PM by Ram Kandimalla

10,000 records.  What will you do with 10,000 records?  You should never in a situation to get 10,000 records of anything from database server to web server and work them in your code-behind.

Say you are displaying 100 at a time.  Your query (propagated from the web page to your butoon-click-handling C# code to teh database server) should receive the index of the next 100 records, e.g., 201-300.

# re: Get JSON from SQL Server

Sunday, August 29, 2010 4:29 AM by Harry

clr json integration

www.sql-library.com

# re: Get JSON from SQL Server

Monday, October 11, 2010 3:30 PM by Gustavo

Thiago, quando rodo a SP o string que vem é nulo...

# Hacking: DevSummit Mobile Agenda Web | blog.davebouwman.com

Pingback from  Hacking: DevSummit Mobile Agenda Web | blog.davebouwman.com

# re: Get JSON from SQL Server

Sunday, May 29, 2011 1:03 AM by weblogs.asp.net

Get json from sql server.. Outstanding :)

Leave a Comment

(required) 
(required) 
(optional)
(required)