I am trying to write a script component to parse the list of JSON and put it in database with the help of Source Script Component.
Sample data looks something like this :
[{
"name": "FP(ETU)-75P",
"id": 6524,
"categoryName": "Accelerator",
"categoryId": 7,
"specificGravity": "0.87"
},
{
"name": "Mold Type",
"id": 6238,
"categoryName": null,
"categoryId": null,
"specificGravity": null
},
{
"name": "Press Type",
"id": 6239,
"categoryName": null,
"categoryId": null,
"specificGravity": null
},
{
"name": "Screw Temperature",
"id": 6241,
"categoryName": null,
"categoryId": null,
"specificGravity": null
}]
So far I have this error while executing,
Since there are nulls with every different type of outputs (String, Float and Int), what is the best way to handle this.
Also, this is what I have so far,
#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Net;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
using System.Web.Script.Serialization;
#endregion
#region Class
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
/// <summary>Outputs records to the output buffer</summary>
public override void CreateNewOutputRows()
{
//Set Webservice URL
string wUrl = "https://app.uncountable.com/api/materials/6/3/0/customer/get_internal_data?apiId=2cde7aa2b98611acbb40fc5bc365b8be1cbca94a26ce6649301e8300d&apiSecretKey=b34f63919ff886d9c280ddab7fb7cef2f6d08634cad40122db9f05bcc333&data=%7b%22dataType%22:%20%22ingredients%22%7d";
try
{
//Call getWebServiceResult to return our WorkGroupMetric array
Ingredient[] outPutMetrics = GetWebServiceResult(wUrl);
//For each group of metrics output records
foreach (var metric in outPutMetrics)
{
Output0Buffer.AddRow();
Output0Buffer.name = metric.name;
// Output0Buffer.CategoryName = metric.CategoryName;
Output0Buffer.id = metric.id;
// TRIM(Output0Buffer.SpecificGravity) = metric.specificGravity;
}
}
catch (Exception e)
{
FailComponent(e.ToString());
}
}
/// <summary>
/// Method to return our WorkGroupMetric array
/// </summary>
/// <param name="wUrl">The web service URL to call</param>
/// <returns>An array of WorkGroupMetric composed of the de-serialized JSON</returns>
private Ingredient[] GetWebServiceResult(string wUrl)
{
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl);
HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse();
Ingredient[] 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<Ingredient[]>(jsonString);
}
//Output connection error message
else
{
FailComponent(httpWResp.StatusCode.ToString());
}
}
//Output JSON parsing error
catch (Exception e)
{
FailComponent(e.ToString());
}
return jsonResponse;
}
/// <summary>
/// Outputs error message
/// </summary>
/// <param name="errorMsg">Full error text</param>
private void FailComponent(string errorMsg)
{
bool fail = false;
IDTSComponentMetaData100 compMetadata = this.ComponentMetaData;
compMetadata.FireError(1, "Error Getting Data From Webservice!", errorMsg, "", 0, out fail);
}
}
#endregion
#region JSON Class
//Class to hold our work group metrics
class Ingredient
{
public string name { get; set; }
public string CategoryName { get; set; }
public int id { get; set; }
public float specificGravity { get; set; }
public int categoryId { get; set; }
}
#endregion
thanks in advance