1

I have a JSON file with a lot of data, and I want to put that JSON data into a SQL Server database. I am very new to this and I don't know where to start. I want to parse the JSON data in a way I can then send '{title:"""}' into my database column 'title', and so on for each column. Here is what the JSON data looks like.

{"success":true,"data":[
  {
    "Title": "text here",
    "Description": "text here",
    "Order Type": "text here",
    "Date": "text here"
  },
  {
    "Title": "text",
    "Description": "text",
    "Order Type": "text",
    "Date": "text"
  },

I'm still trying to figure out the best way to approach this but I'm in visual studio using webpages. I have JSON.NET but don't know where to start as far as decoding the json file. But from there how do I send each line in the object to the corresponding database column? Any advice would help as I see I need to go back to school lol

3

1 Answer 1

2

If you have SQL Server 2016, this may suit you:

SET @json =
N'[
      {"success":true,"data":[
        {
          "Title": "text here",
          "Description": "text here",
          "Order Type": "text here",
          "Date": "text here"
        },
        {
          "Title": "text",
          "Description": "text",
          "Order Type": "text",
          "Date": "text"
        }]
    }
]'

INSERT INTO YourTable
SELECT jsonData.*
FROM OPENJSON (@json, N'$.data')
          WITH (
             Title   varchar(200) N'$.data.Title', 
             Date     varchar(200)     N'$.data.Description',
             Customer varchar(200) N'$.data.Order Type', 
             Quantity varchar(200)          N'$.data.Date'
          )
 AS jsonData;
Sign up to request clarification or add additional context in comments.

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.