2

I have a JSON file. In that file around 1000 records. I am using C# and SQL Server.

My requirement is I want to store JSON file's records in to my table which created in SQL Server.

Can anyone tell me how to store JSON data in SQL Server using c# or JavaScript or jQuery anything?

5
  • Check this: blogs.msdn.microsoft.com/dilkushp/2013/10/31/… Commented Sep 9, 2016 at 6:39
  • What SQL engine are you using? Commented Sep 9, 2016 at 6:39
  • sql server 2012 Commented Sep 9, 2016 at 6:44
  • 1
    i suggest you should create an object (or objects) based on your Json and using JSON.NET to convert it stackoverflow.com/questions/2246694/… Commented Sep 9, 2016 at 7:04
  • Convert JSON to XML and save in SQL 2012 XML DataType. You can parse vice-versa from XML to JSON if needed Or JSON can be saved as plain text (not recommended). Commented Sep 9, 2016 at 7:05

2 Answers 2

1

This questions is a little old but here's an example. If you have SQL Server 2016 you can use a lot of built-in json functions SQL Server Json Support

Google newtonsoft json for examples (or whatever library you're using). I'm using the Newtonsoft.Json library. Make sure it's referenced in your project or add it via Nuget.

Your class should have a reference to it:

  using Newtonsoft.Json;

Converting C# list of ChartModel into json. The model (any POCO works) is defined as:

  public class ChartModel
  {
    public string ChartType { get; set; }
    public IList<ChartSeries> Data { get; set; }
    public string ChartTitle { get; set; }
    public int DisplayOrder { get; set; }
  }

Convert List to its json version:

  // Charts = List<ChartModel> 
  var chartJson = JsonConvert.SerializeObject(Charts);

  // or if you want the json formatted
  var chartJson = JsonConvert.SerializeObject(products, Formatting.Indented);

Now you have your json, a string, that can be stored in any nvarchar defined column.

Sign up to request clarification or add additional context in comments.

Comments

0

It can be done by many ways. It depends on your need. if you do it very frequently then

  1. Simply upload the file on the server then process a schedule a job
    which processes all the record.

  2. Post your file on Server using Jquery then convert it to XML and pass it as a XML parameter to SQL Server procedure and then run insert command. This is not recommended for this type of bulk operation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.