SSIS 脚本组件 JSON 到 SQL 服务器 table

SSIS Script Component JSON to SQL Server table

我构建了一个包,每次 运行 它都会更新一个 JSON 文件;下一阶段是将单行数据加载到 table.

我正在使用脚本任务加载它。

以下是我的 json 文件: https://query2.finance.yahoo.com/v7/finance/quote?symbols=AAPL

脚本组件需要将不同的 key-value 对识别为列 headers 和列值。

相应地配置了输出:

这是我的脚本任务 C# 脚本,在构建操作时没有给出任何错误:

#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Web.Script.Serialization;
using Microsoft.SqlServer.Dts.Pipeline;
#endregion

namespace SC_ce793b89c26a4be48b4f5354892c6d24
{ 
    [Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
    public class ScriptMain : UserComponent
    {
        public override void PreExecute()
        {
            base.PreExecute();
        }

        public override void PostExecute()
        {
            base.PostExecute();
        }

        class result
        {
            public string language { get; set;}
            public string quoteType { get; set; }
            public string quoteSourceName { get; set; }
            public string currency { get; set; }
            public decimal sharesOutstanding { get; set; }
            public decimal bookValue { get; set; }
            public decimal fiftyDayAverage { get; set; }
            public decimal epsTrailingTwelveMonths { get; set; }
            public decimal forwardPE { get; set; }
            public decimal priceToBook { get; set; }
            public decimal trailingAnnualDividendYield { get; set; }
            public string tradeable { get; set; }
            public decimal fiftyDayAverageChange { get; set; }
            public decimal fiftyDayAverageChangePercent { get; set; }
            public decimal twoHundredDayAverage { get; set; }
            public decimal twoHundredDayAverageChange { get; set; }
            public decimal priceHint { get; set; }
            public decimal fiftyTwoWeekLowChange { get; set; }
            public string fullExchangeName { get; set; }
            public string longName { get; set; }
            public string financialCurrency { get; set; }
            public decimal averageDailyVolume3Month { get; set; }
            public decimal averageDailyVolume10Day { get; set; }
            public decimal fiftyTwoWeekLow { get; set; }
            public decimal fiftyTwoWeekHigh { get; set; }
            public decimal dividendDate { get; set; }
            public string shortName { get; set; }
            public string marketState { get; set; }
            public decimal regularMarketChangePercent { get; set; }
            public decimal regularMarketPreviousClose { get; set; }
            public decimal bid { get; set; }
            public decimal ask { get; set; }
            public decimal bidSize { get; set; }
            public decimal askSize { get; set; }
            public string messageBoardId { get; set; }
            public decimal epsForward { get; set; }
            public decimal twoHundredDayAverageChangePercent { get; set; }
            public decimal marketCap { get; set; }
            public decimal exchangeDataDelayedBy { get; set; }
            public string exchange { get; set; }
            public decimal regularMarketPrice { get; set; }
            public decimal regularMarketTime { get; set; }
            public decimal regularMarketChange { get; set; }
            public decimal regularMarketOpen { get; set; }
            public decimal regularMarketDayHigh { get; set; }
            public decimal regularMarketDayLow { get; set; }
            public decimal regularMarketVolume { get; set; }
            public string market { get; set; }
            public decimal sourceInterval { get; set; }
            public string exchangeTimezoneName { get; set; }
            public string exchangeTimezoneShortName { get; set; }
            public decimal gmtOffSetMilliseconds { get; set; }
            public decimal earningsTimestamp { get; set; }
            public decimal earningsTimestampStart { get; set; }
            public decimal earningsTimestampEnd { get; set; }
            public decimal trailingAnnualDividendRate { get; set; }
            public decimal trailingPE { get; set; }
            public decimal fiftyTwoWeekLowChangePercent { get; set; }
            public decimal fiftyTwoWeekHighChange { get; set; }
            public decimal fiftyTwoWeekHighChangePercent { get; set; }
            public string symbol { get; set; }
        }

        class quoteResponse
        { 
            public result result { get; set; }
        }

        public override void Input0_ProcessInputRow(Input0Buffer Row)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();

            BlobColumn combinedColumn = Row.Column0;

            string reviewConverted = System.Text.Encoding.ASCII.GetString(combinedColumn.GetBlobData(0, Convert.ToInt32(combinedColumn.Length)));

            quoteResponse quoteResponse = js.Deserialize<quoteResponse>(reviewConverted);

            Row.regularMarketChangePercent = quoteResponse.result.regularMarketChangePercent;
            Row.regularMarketChange = quoteResponse.result.regularMarketChange;
            Row.regularMarketDayHigh = quoteResponse.result.regularMarketDayHigh;
            Row.regularMarketDayLow = quoteResponse.result.regularMarketDayLow;
            Row.epsTrailingTwelveMonths = quoteResponse.result.epsTrailingTwelveMonths;
            Row.regularMarketPrice = quoteResponse.result.regularMarketPrice;
            Row.marketCap = quoteResponse.result.marketCap;
            Row.trailingPE = quoteResponse.result.trailingPE;
            Row.symbol = quoteResponse.result.symbol;
            Row.regularMarketVolume = quoteResponse.result.regularMarketVolume;
            Row.fiftyTwoWeekHigh = quoteResponse.result.fiftyTwoWeekHigh;
            Row.fiftyTwoWeekLow = quoteResponse.result.fiftyTwoWeekLow;
            Row.xask = quoteResponse.result.ask;
            Row.xaskSize = quoteResponse.result.askSize;
            Row.xaverageDailyVolume10Day = quoteResponse.result.averageDailyVolume10Day;
            Row.xaverageDailyVolume3Month = quoteResponse.result.averageDailyVolume3Month;
            Row.xbid = quoteResponse.result.bid;
            Row.xbidSize = quoteResponse.result.bidSize;
            Row.xbookValue = quoteResponse.result.bookValue;
            Row.xcurrency = quoteResponse.result.currency;
            Row.xdividendDate = quoteResponse.result.dividendDate;
            Row.xearningsTimestamp = quoteResponse.result.earningsTimestamp;
            Row.xearningsTimestampEnd = quoteResponse.result.earningsTimestampEnd;
            Row.xearningsTimestampStart = quoteResponse.result.earningsTimestampStart;
            Row.xepsForward = quoteResponse.result.epsForward;
            Row.xexchange = quoteResponse.result.exchange;
            Row.xexchangeDataDelayedBy = quoteResponse.result.exchangeDataDelayedBy;
            Row.xexchangeTimezoneName = quoteResponse.result.exchangeTimezoneName;
            Row.xexchangeTimezoneShortName = quoteResponse.result.exchangeTimezoneShortName;
            Row.xfiftyDayAverage = quoteResponse.result.fiftyDayAverage;
            Row.xfiftyDayAverageChange = quoteResponse.result.fiftyDayAverageChange;
            Row.xfiftyDayAverageChangePercent = quoteResponse.result.fiftyDayAverageChangePercent;
            Row.xfiftyTwoWeekHighChange = quoteResponse.result.fiftyTwoWeekHighChange;
            Row.xfiftyTwoWeekHighChangePercent = quoteResponse.result.fiftyTwoWeekHighChangePercent;
            Row.xfiftyTwoWeekLowChange = quoteResponse.result.fiftyTwoWeekLowChange;
            Row.xfiftyTwoWeekLowChangePercent = quoteResponse.result.fiftyTwoWeekLowChangePercent;
            Row.xfinancialCurrency = quoteResponse.result.financialCurrency;
            Row.xforwardPE = quoteResponse.result.forwardPE;
            Row.xfullExchangeName = quoteResponse.result.fullExchangeName;
            Row.xgmtOffSetMilliseconds = quoteResponse.result.gmtOffSetMilliseconds;
            Row.xlanguage = quoteResponse.result.language;
            Row.xlongName = quoteResponse.result.longName;
            Row.xmarket = quoteResponse.result.market;
            Row.xmarketState = quoteResponse.result.marketState;
            Row.xmessageBoardId = quoteResponse.result.messageBoardId;
            Row.xpriceHint = quoteResponse.result.priceHint;
            Row.xpriceToBook = quoteResponse.result.priceToBook;
            Row.xquoteSourceName = quoteResponse.result.quoteSourceName;
            Row.xquoteType = quoteResponse.result.quoteType;
            Row.xregularMarketOpen = quoteResponse.result.regularMarketOpen;
            Row.xregularMarketPreviousClose = quoteResponse.result.regularMarketPreviousClose;
            Row.xregularMarketTime = quoteResponse.result.regularMarketTime;
            Row.xsharesOutstanding = quoteResponse.result.sharesOutstanding;
            Row.xshortName = quoteResponse.result.shortName;
            Row.xsourceInterval = quoteResponse.result.sourceInterval;
            Row.xtradeable = quoteResponse.result.tradeable;
            Row.xtrailingAnnualDividendRate = quoteResponse.result.trailingAnnualDividendRate;
            Row.xtrailingAnnualDividendYield = quoteResponse.result.trailingAnnualDividendYield;
            Row.xtwoHundredDayAverage = quoteResponse.result.twoHundredDayAverage;
            Row.xtwoHundredDayAverageChange = quoteResponse.result.twoHundredDayAverageChange;
            Row.xtwoHundredDayAverageChangePercent = quoteResponse.result.twoHundredDayAverageChangePercent;

            Row.regularMarketChangePercent_IsNull = true;
            Row.regularMarketChange_IsNull = true;
            Row.regularMarketDayHigh_IsNull = true;
            Row.regularMarketDayLow_IsNull = true;
            Row.epsTrailingTwelveMonths_IsNull = true;
            Row.regularMarketPrice_IsNull = true;
            Row.marketCap_IsNull = true;
            Row.trailingPE_IsNull = true;
            Row.symbol_IsNull = true;
            Row.regularMarketVolume_IsNull = true;
            Row.fiftyTwoWeekHigh_IsNull = true;
            Row.fiftyTwoWeekLow_IsNull = true;
            Row.xask_IsNull = true;
            Row.xaskSize_IsNull = true;
            Row.xaverageDailyVolume10Day_IsNull = true;
            Row.xaverageDailyVolume3Month_IsNull = true;
            Row.xbid_IsNull = true;
            Row.xbidSize_IsNull = true;
            Row.xbookValue_IsNull = true;
            Row.xcurrency_IsNull = true;
            Row.xdividendDate_IsNull = true;
            Row.xearningsTimestamp_IsNull = true;
            Row.xearningsTimestampEnd_IsNull = true;
            Row.xearningsTimestampStart_IsNull = true;
            Row.xepsForward_IsNull = true;
            Row.xexchange_IsNull = true;
            Row.xexchangeDataDelayedBy_IsNull = true;
            Row.xexchangeTimezoneName_IsNull = true;
            Row.xexchangeTimezoneShortName_IsNull = true;
            Row.xfiftyDayAverage_IsNull = true;
            Row.xfiftyDayAverageChange_IsNull = true;
            Row.xfiftyDayAverageChangePercent_IsNull = true;
            Row.xfiftyTwoWeekHighChange_IsNull = true;
            Row.xfiftyTwoWeekHighChangePercent_IsNull = true;
            Row.xfiftyTwoWeekLowChange_IsNull = true;
            Row.xfiftyTwoWeekLowChangePercent_IsNull = true;
            Row.xfinancialCurrency_IsNull = true;
            Row.xforwardPE_IsNull = true;
            Row.xfullExchangeName_IsNull = true;
            Row.xgmtOffSetMilliseconds_IsNull = true;
            Row.xlanguage_IsNull = true;
            Row.xlongName_IsNull = true;
            Row.xmarket_IsNull = true;
            Row.xmarketState_IsNull = true;
            Row.xmessageBoardId_IsNull = true;
            Row.xpriceHint_IsNull = true;
            Row.xpriceToBook_IsNull = true;
            Row.xquoteSourceName_IsNull = true;
            Row.xquoteType_IsNull = true;
            Row.xregularMarketOpen_IsNull = true;
            Row.xregularMarketPreviousClose_IsNull = true;
            Row.xregularMarketTime_IsNull = true;
            Row.xsharesOutstanding_IsNull = true;
            Row.xshortName_IsNull = true;
            Row.xsourceInterval_IsNull = true;
            Row.xtradeable_IsNull = true;
            Row.xtrailingAnnualDividendRate_IsNull = true;
            Row.xtrailingAnnualDividendYield_IsNull = true;
            Row.xtwoHundredDayAverage_IsNull = true;
            Row.xtwoHundredDayAverageChange_IsNull = true;
            Row.xtwoHundredDayAverageChangePercent_IsNull = true;
        }
    }
}

我在构建时没有收到任何错误,但在执行时收到错误:

at SC_ce793b89c26a4be48b4f5354892c6d24.ScriptMain.Input0_ProcessInputRow(Input0Buffer Row) at UserComponent.Input0_ProcessInput(Input0Buffer Buffer) at UserComponent.ProcessInput(Int32 InputID, String InputName, PipelineBuffer Buffer, OutputNameMap OutputMap) at Microsoft.SqlServer.Dts.Pipeline.ScriptComponent.ProcessInput(Int32 InputID, PipelineBuffer buffer) at Microsoft.SqlServer.Dts.Pipeline.ScriptComponentHost.ProcessInput(Int32 inputID, PipelineBuffer buffer)

据我所知,我没有做错任何事,只是无法将这件事做到 运行。感谢任何帮助。

我不确定您实际上遇到了什么错误,因为您在问题中显示的内容似乎是堆栈跟踪的一部分,而不是异常。

但是,我愿意打赌实际问题是 NullReferenceExecption,因为 quoteResponse 上的 result 对象为空。 result 为 null 的原因是因为您尝试反序列化的 class 结构与您正在反序列化的 JSON 的结构不匹配。

特别是,我看到了两个问题:

  1. 在JSON中,result值是一个数组(括在方括号[]中),但在你的quoteResponseclass 您已将其定义为单个对象。
  2. 在JSON中,quoteResponse是在一个外部对象里面。您缺少此对象的 class。

要修复,请将现有的 quoteResponse class 替换为这些 classes:

class quoteResponse
{
    public List<result> result { get; set; }
}

class rootObject
{
    public quoteResponse quoteResponse { get; set; }
}

然后,将 JSON 反序列化为 rootObject class 而不是 quoteResponse class:

rootObject root = js.Deserialize<rootObject>(reviewConverted);

从那里,您可以获得这样的 result

result result = root.quoteResponse.result[0];

然后您可以根据需要从结果中提取各个属性:

Row.regularMarketChangePercent = result.regularMarketChangePercent;
Row.regularMarketChange = result.regularMarketChange;
// etc.