简体   繁体   中英

How can i insert JSON into Azure SQL DB in Node.js (REST API)

I am trying to build new REST API with few get\post methods and my question is: How can i struct my table to support json? and what i should write to insert the spesific json that i get from user to my DB

DB.js:

require("dotenv").config();
const sql = require("mssql")
// Create connection to database
const config = {
      userName: process.env.tedious_userName,
      password: process.env.tedious_password,
      server:process.env.tedious_server,
      database:process.env.tedious_database
     };

const connection = new Connection(config);

// Attempt to connect and execute queries if connection goes through

module.exports =connection.on("connect", err => {
  if (err) {
    console.error(err.message);
});

module.exports = connectDB 

now i want to create some post method of getting new recipe from the user, so i get this JSON:

[
  {
    "username": "newuser",
    "id": 1,
    "name": "Hamburger",
    "img": "https://image.shutterstock.com/w-705104968.jpg",
    "time": 45,
    "likes": 17,
    "isGluten": false,
    "isVegaterian": false,
    "isWatched": false,
    "isSave": false,
    "ingredients": [
      {
        "amount": 5,
        "product": "pound beef short ribs"
      },
      {
        "amount": 2,
        "product": "teaspoon salt"
      },
      {
        "amount": 1.5,
        "product": "tablespoons all-purpose flour"
      },
      {
        "amount": 0.5,
        "product": "teaspoon ground black pepper"
      }
    ],
    "instructions": [
      {
        "Step": "Preheat oven to 350 degrees F (175 degrees C). Grease and flour a 9x9 inch pan or line a            muffin pan with paper liners."
      },
      {
        "Step": "In a medium bowl, cream together the sugar and butter. Beat in the eggs, one at a time, then stir in the vanilla. Combine flour and baking powder, add to the creamed mixture and mix well. Finally stir in the milk until batter is smooth. Pour or spoon batter into the prepared pan."
      },
      {
        "Step": "Bake for 30 to 40 minutes in the preheated oven. For cupcakes, bake 20 to 25 minutes. Cake is done when it springs back to the touch."
      }
    ]
  }
]

I need some help with define table that supports json files and insert this json above to this table.

1.You may create table like below:

create table myTable
 (
  Id int identity primary key,
  Data nvarchar(max) 
 )

the Data column is where your JSON data stored.

2.Create stored procedure like below:

create procedure InsertJSON(@json nvarchar(max))
 as begin
  insert into myTable(Data)
  values(@json)
 end

3.Execute stored procedure

eg.

exec InsertJSON '{"Price":10455,"Color":"White","tags": ["toy","children","games"]}'

and check if JSON data has been stored into myTable

4.Try to query JSON data with built-in JSON_VALUE()、JSON_QUERY()

select JSON_VALUE(Data, '$.Price') from myTable

Finally you may check out this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM