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?

Termininja
  • 6,620
  • 12
  • 48
  • 49
sayali
  • 255
  • 2
  • 3
  • 8
  • Check this: https://blogs.msdn.microsoft.com/dilkushp/2013/10/31/easiest-way-of-loading-json-data-in-sql-using-c/ – Rahul Tripathi Sep 09 '16 at 06:39
  • What SQL engine are you using? – Grzegorz B. Sep 09 '16 at 06:39
  • sql server 2012 – sayali Sep 09 '16 at 06:44
  • 1
    i suggest you should create an object (or objects) based on your Json and using JSON.NET to convert it http://stackoverflow.com/questions/2246694/how-to-convert-json-object-to-custom-c-sharp-object#2246724 – Jacky Sep 09 '16 at 07: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). – Gaurav P Sep 09 '16 at 07:05

2 Answers2

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.

Will Lopez
  • 2,089
  • 3
  • 40
  • 64
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

Mohit Dagar
  • 522
  • 6
  • 21