Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
SQL
Hello!

I want to convert my SQL server table data into JSON format, please suggest how to do that?
:)
Posted
Updated 17-Aug-14 1:14am
v2
Comments
Sergey Alexandrovich Kryukov 15-Aug-14 16:53pm    
Not a question. Suggest what? The solution depends on the data structure you want to pass and perhaps some requirements for JSON structure, if any. Also, do you use any high-level computer language except SQL, on your client software? Or do you want to form JSON in SQL only (is so, why?)?
—SA
Abhishek Jaiswall 17-Aug-14 23:50pm    
I have a data table stored in my sql server and i wanna convert it into JSON for creating a chart from the JSON data(using D3JS). but for now I only want to convert sql server data into JSON.
suppose my table name is salary (EmpName, Salary) now my query is how to parse this data table into JSON for further use.
Sergey Alexandrovich Kryukov 18-Aug-14 1:19am    
Why did you ignore my question on the language? In some other comment, you mentioned C#...
—SA
Abhishek Jaiswall 18-Aug-14 3:12am    
Oops, sorry i want that in C#.
Sergey Alexandrovich Kryukov 18-Aug-14 3:27am    
All right. Now, did you get the helpful answer?
Data contracts are good when you both write JSON and read it. This is not always the case. If your JSON is just given (could be defined by other system not using Data Contact, the approach would be different. Is you define your JSON structure, I would try to advise.
—SA

Create a class that mirrors your table. The DataConract and DataMember are needed to make this work. The solution is written in VB.NET

VB
<datacontract> _
Class TableName
	<datamember> _
	Friend ColumnOne As String

	<datamember> _
	Friend ColumnTwo As Integer
End Class


Load data into the class. Then call the json serializer

VB
Dim oNewTable As New TableName
Using oMemStream As New IO.MemoryStream()
    Dim oJsonSerializer As New Json.DataContractJsonSerializer(GetType(TableName))
    oJsonSerializer.WriteObject(oMemStream, oNewTable)
    Dim szJsonString As String = System.Text.Encoding.ASCII.GetString(oMemStream.ToArray())
End Using


Json Reference
 
Share this answer
 
v3
Comments
Abhishek Jaiswall 17-Aug-14 7:14am    
I want that in C#.
meanwhile thnks! :)
SteveyJDay 18-Aug-14 12:14pm    
http://converter.telerik.com/
if you asp.net with c# behindcode you can use sqldatasource on aspx and on codebehind write this code:
C#
protected void Page_Load(object sender, EventArgs e)
        {
            DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
            DataTable dt = dv.ToTable();
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            List<dictionary><string,>> rows = new List<dictionary><string,>>();
            Dictionary<string,> row;
            foreach (DataRow dr in dt.Rows)
            {
                row = new Dictionary<string,>();
                foreach (DataColumn col in dt.Columns)
                {
                    row.Add(col.ColumnName, dr[col]);
                }
                rows.Add(row);
            }
                string data = serializer.Serialize(rows);
                string script= String.Format("<script type=\"text/javascript\">var jsondata = {0}</script>",data);
                if (!this.ClientScript.IsClientScriptBlockRegistered("datascript"))
                {
                    this.ClientScript.RegisterClientScriptBlock(this.GetType(), "datascript", script);
                }
            }
        }

Don't forget to add register script block on aspx page.
 
Share this answer
 
v2
Comments
Abhishek Jaiswall 17-Aug-14 7:17am    
what's that register script block?
Abhishek Jaiswall 18-Aug-14 0:55am    
I can write Sqldatasource on codebehind or on webconfig file too right! What I want to know is what does that means- 'Add register script block on aspx page ?'
widy angjaya 18-Aug-14 6:51am    
usually i write sqldatasource on aspx.
register script block is like ajax script, that make your json go to your page as javascript.
widy angjaya 18-Aug-14 7:26am    
for register script block, you can add this script below on aspx inside form tag
<asp:ScriptManager ID="ClientScript1" runat="server">
Abhishek Jaiswall 18-Aug-14 9:10am    
Okay , thanks let me try this!
JSON serializer is only good if you both write and read the data. All serializers allow you to store some object graph to a stream and later restore if from stream. Roughly speaking, if you stored JSON once, you can figure out its structure and write JSON manually, following this structure, or write code using different system, not using Data Contract (for example, because this is not .NET).

But if JSON is given, you need to follow its structure. The approach when you mirror your database schema also may not work, because that given JSON wasn't designed for your database. Then you can design some .NET type to follow the given JSON. I explained it in my past answers:
How To Convert object type to C# class object type[^],
pass json data to javascript?[^].

I must say, you did not provide enough information and didn't answer my question, so the answers are based on too much of guesswork.

—SA
 
Share this answer
 
Comments
Abhishek Jaiswall 18-Aug-14 14:08pm    
What information you need to answer my query, please let me know ?
Sergey Alexandrovich Kryukov 18-Aug-14 16:08pm    
I already answered your question, or complemented the other answers. But you did not answer my questions I already asked you in my comment to the question.
If you need more detail, you would need to explain where JSON comes from (because if its structure is not under your control), and, in this case, explain its structure, perhaps show a sample.
If this is your code which generates JSON, you would need to explain why the answers about Data Contract does not suite you.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900