[英]How to import data from REST API into SQL Server?
I have a REST API that returns a response formatted in JSON, something like this : 我有一个REST API,它返回以JSON格式格式化的响应,如下所示:
{
"Employees" : [
{
"userId":"romin",
"jobTitleName":"Developer",
"emailAddress":"romin.k.irani@gmail.com"
},
{
"userId":"nirani",
"jobTitleName":"Developer",
"emailAddress":"neilrirani@gmail.com"
},
{
"userId":"tomhan",
"jobTitleName":"Program Directory",
"emailAddress":"tomhanks@gmail.com"
}
]
}
Is there any built-in functions that could help in creating a table "Employees" in an SQL Server database from the JSON above ? 是否有任何内置函数可以帮助通过上述JSON在SQL Server数据库中创建表“ Employees”?
+--------+-------------------+-------------------------+
| UserId | jobTitleName | emailAddress |
+--------+-------------------+-------------------------+
| romin | Developer | romin.k.irani@gmail.com |
| nirani | Developer | neilrirani@gmail.com |
| tomhan | Program Directory | tomhanks@gmail.com |
+--------+-------------------+-------------------------+
first create C# class according to your JSON 首先根据您的JSON创建C#类
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public partial class Welcome
{
[JsonProperty("Employees")]
public Employee[] Employees { get; set; }
}
public partial class Employee
{
[JsonProperty("userId")]
public string UserId { get; set; }
[JsonProperty("jobTitleName")]
public string JobTitleName { get; set; }
[JsonProperty("emailAddress")]
public string EmailAddress { get; set; }
}
var JSONObj= JsonConvert.DeserializeObject<Welcome>(json); //convert you json in to class object
var EmpArray=JSONObj["Employees"]; // will give you employee array
//Now you can make an iteration over there to insert your data //现在您可以在那里进行迭代以插入数据
You can use the OPENJSON table-valued function to parse JSON data into rows and columns. 您可以使用OPENJSON表值函数将JSON数据解析为行和列。
DECLARE @json VARCHAR(max) ='
{
"Employees" : [
{
"userId":"romin",
"jobTitleName":"Developer",
"emailAddress":"romin.k.irani@gmail.com"
},
{
"userId":"nirani",
"jobTitleName":"Developer",
"emailAddress":"neilrirani@gmail.com"
},
{
"userId":"tomhan",
"jobTitleName":"Program Directory",
"emailAddress":"tomhanks@gmail.com"
}
]
}
'
SELECT userId,
jobTitleName,
emailAddress
FROM OPENJSON(@Json, '$.Employees')
WITH (
userId VARCHAR(100),
jobTitleName VARCHAR(100),
emailAddress VARCHAR(100)
);
Is the response formatted in JSON self-descriptive? 响应是否采用JSON自描述格式? You probably want to write your own client cache if you have a lot of fun with hypermedia. 如果您对超媒体很感兴趣,那么您可能想编写自己的客户端缓存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.