简体   繁体   中英

SSIS Script Component JSON to SQL Server table

I have built a package that updates a JSON file every time I run it; the next stage is to load a single row of data to a table.

I am using a script task to load it.

Following is my json file: https://query2.finance.yahoo.com/v7/finance/quote?symbols=AAPL

The script component needs to identify the different key-value pairs as column headers and column values.

Configured the outputs accordingly:

SSIS输出配置

Here is my script task C# script that gives no error on build action:

#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;
        }
    }
}

I do not get any error on build, but I get one on execute:

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)

As far as I know I am not doing anything wrong, just can't get this thing to run. Any help is appreciated.

I'm not sure what error you are actually getting because what you show in your question appears to be part of a stack trace rather than the exception.

However, I'd be willing to bet that the actual problem is a NullReferenceExecption due to the result object on quoteResponse being null. And the reason that result is null is because the class structure that you are trying to deserialize into does not match the structure of the JSON you are deserializing.

In particular, there are two problems that I see:

  1. In the JSON, the result value is an array (enclosed in square brackets [ and ] ), but in your quoteResponse class you have defined it as a single object.
  2. In the JSON, quoteResponse is inside an outer object. You are missing a class for this object.

To fix, replace your existing quoteResponse class with these classes:

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

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

Then, deserialize the JSON into the rootObject class instead of the quoteResponse class:

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

From there, you can get the result like this:

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

And then you can extract the individual properties from the result as needed:

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

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