You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SSIS中从API获取JSON数据至SQL Server的字段提取问题

SSIS API JSON数据提取解决方案

一、records字段的正确数据类型

在Script Component(作为数据源使用时),records是JSON数组结构,需将其对应输入列的DataType设置为**DT_NTEXT**(优先选择,支持大文本内容)或DT_TEXT,确保能完整保存数组的JSON原始文本,避免因长度限制导致截断。

二、解决“Invalid JSON primitive: https”错误

该错误是因为反序列化时传入的不是合法JSON文本(大概率误传了API URL而非响应内容),调整代码步骤如下:

  1. 确保已通过HTTP请求获取到完整的API响应JSON字符串,而非URL本身。
  2. 使用自定义类匹配JSON结构,再进行反序列化:
// 定义匹配API返回结构的类
public class ApiResponse
{
    public string HREF { get; set; }
    public int recordcount { get; set; }
    public string previouspage { get; set; }
    public List<RecipientItem> records { get; set; }
}

public class RecipientItem
{
    public string Recipient { get; set; }
    // 补充JSON中records数组里的其他字段,如Email、RecipientID等
}

// 在Script Component的CreateNewOutputRows方法中执行反序列化
string rawJson = Variables.ApiFullResponse; // 假设API响应已存入SSIS变量
ApiResponse parsedResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<ApiResponse>(rawJson);

// 遍历records数组生成输出行
foreach (var item in parsedResponse.records)
{
    Output0Buffer.AddRow();
    Output0Buffer.Recipient = item.Recipient;
    // 将其他字段赋值到对应的输出列
}
  • 注意:若未引用Newtonsoft.Json库,需在Script Component的“项目”→“添加引用”中导入该库;也可使用System.Text.Json替代,语法逻辑一致。

三、将records数据写入SQL Server表

  1. 在Script Component的“输入和输出”设置中,添加与RecipientItem类字段对应的输出列,设置匹配的数据类型(如Recipient设为DT_WSTR,长度按需配置)。
  2. 在数据流任务中添加OLE DB目标组件,连接目标SQL Server数据库,选择要写入的表。
  3. 进入OLE DB目标的“映射”界面,将Script Component的输出列与SQL表的对应字段一一映射,确保数据类型兼容(如SSIS的DT_WSTR对应SQL的NVARCHAR)。

内容的提问来源于stack exchange,提问作者Rana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 02:05:18