I am new to importing API's using script task in SSIS. I have to manage the import of a straightforward API JSON file but this second API JSON file is a bit tricky. I have been looking at the code and just don't know what I'm doing wrong.
My JSON file has a header which I need to determine how many pages of the API I need to loop through to get the data, but where I'm lost is, how do you import the next bit of data. I think I am close but since I'm new to this I could do it with a bit of guidance.
My code in the script task below, the have seen the stream reader import the data but I do not what use (e.g List, class or dictionary)to import the countIn
, countOut
, etc columns.
try
{
//Call getWebServiceResult to return our WorkGroupMetric array
WorkGroupMetric[] outPutMetrics = GetWebServiceResult(wUrl);
//For each group of metrics output records
foreach (var metric in outPutMetrics)
{
APIBuffer.AddRow();
APIBuffer.count = metric.count;
APIBuffer.currentpage = metric.currentpage;
APIBuffer.totalpages = metric.totalpages;
APIBuffer.countIn = metric.result.countIn;
APIBuffer.countOut = metric.result.countOut;
APIBuffer.type = metric.result.type;
APIBuffer.countLine = metric.result.countLine;
APIBuffer.from = metric.result.from;
APIBuffer.to = metric.result.to;
}
}
catch (Exception e)
{
FailComponent(e.ToString());
}
}
private WorkGroupMetric[] GetWebServiceResult(string wUrl)
{
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl);
HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse();
WorkGroupMetric[] jsonResponse = null;
try
{
//Test the connection
if (httpWResp.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = httpWResp.GetResponseStream();
string jsonString = null;
//Set jsonString using a stream reader
using (StreamReader reader = new StreamReader(responseStream))
{
jsonString = reader.ReadToEnd().Replace("\\", "");
reader.Close();
}
//Deserialize our JSON
JavaScriptSerializer sr = new JavaScriptSerializer();
//JSON string comes in with a leading and trailing " that need to be removed for parsing to work correctly
//The JSON here is serialized weird, normally you would not need this trim
jsonResponse = sr.Deserialize<WorkGroupMetric[]>(jsonString.Trim('"'));
}
//Output connection error message
else
{
FailComponent(httpWResp.StatusCode.ToString());
}
}
//Output JSON parsing error
catch (Exception e)
{
FailComponent(e.ToString());
}
return jsonResponse;
throw new NotImplementedException();
}
private void FailComponent(string errorMsg)
{
bool fail = false;
IDTSComponentMetaData100 compMetadata = this.ComponentMetaData;
compMetadata.FireError(1, "Error Getting Data From Webservice!", errorMsg, "", 0, out fail);
}
}
//Class to hold our workgroup metrics
class WorkGroupMetric
{
public string count { get; set; }
public string currentpage { get; set; }
public string totalpages { get; set; }
public List<Result> result { get; set; }
}
class Result
{
public string countIn { get; set; }
public string countOut { get; set; }
public string type { get; set; }
public string countLine { get; set; }
public string from { get; set; }
public string to { get; set; }
}