Import JSON Files to SQL Server with SSIS - json

The JSON file has 2 parts (Nested) and I just could import the first one I don't know why, any idea for this? The first column from the second part showed me NULL in database
This is the JSON file format:
My code is:
public override void CreateNewOutputRows()
{
/*
Add rows by calling the AddRow method on the member variable named "<Output Name>Buffer".
For example, call MyOutputBuffer.AddRow() if your output was named "MyOutput".
*/
String jsonFileContent = File.ReadAllText(#"C:\Json XLM project SSIS\FileSample.JSON");
JavaScriptSerializer js = new JavaScriptSerializer();
List<Order> orders = js.Deserialize<List<Order>>(jsonFileContent);
foreach (Order order in orders)
{
Output0Buffer.AddRow();
Output0Buffer.TrackingNumber = order.TrackingNumber;
Output0Buffer.LocalReferenceNumber = order.LocalReferenceNumber;
Output0Buffer.TotalShipmentPieces = order.TotalShipmentPieces;
Output0Buffer.TotalShipmentValue = order.TotalShipmentValue;
Output0Buffer.ShipmentCurrency = order.ShipmentCurrency;
Output0Buffer.ShipmentGrossWeight = order.ShipmentGrossWeight;
Output0Buffer.ShipmentNetWeight = order.ShipmentNetWeight;
Output0Buffer.BorderTransportMode = order.BorderTransportMode;
Output0Buffer.ConveyanceDetail = order.ConveyanceDetail;
Output0Buffer.TransportId = order.TransportId;
Output0Buffer.Region = order.Region;
Output0Buffer.WarehouseLocation = order.WarehouseLocation;
Output0Buffer.InvoiceNumber = order.InvoiceNumber;
Output0Buffer.InvoiceDate = order.InvoiceDate;
Output0Buffer.ItemCategoryId = order.ItemCategoryId;
Output0Buffer.InsuredValue = order.InsuredValue;
Output0Buffer.SoldByShipmentPartyAddressId = order.SoldByShipmentPartyAddressId;
}
}
Thanks in advance.

Related

SQL query to retrieve data according to train_number

I want to retrieve all the trains with the given coach type that runs between the source and destination from the database in ascending order based on train number;
I am trying this query.This is a JDBC code to fetch the trains. I couldn't figure out what is wrong in this.
`import java.sql.*;
import java.util.*;
import java.io.*;
public class TrainManagementSystem {
public ArrayList <Train> viewTrain (String coachType, String source, String destination){
// Fill your code here
Connection myConn =null;
PreparedStatement myStmt = null;
ResultSet myRes = null;
ArrayList <Train> trainArr = new ArrayList<>();
try{
Properties props = new Properties();
FileInputStream in = new FileInputStream("database.properties");
props.load(in);
in.close();
String driver = props.getProperty("DB_DRIVER_CLASS");
if (driver != null) {
Class.forName(driver) ;
}
myConn = DriverManager.getConnection(
props.getProperty("DB_URL"),
props.getProperty("DB_USERNAME"),
props.getProperty("DB_PASSWORD"));
myStmt = myConn.prepareStatement("SELECT * from train WHERE source = ? AND destination = ? AND ? != 0 ORDER BY train_number");
myStmt.setString(1,source);
myStmt.setString(2,destination);
myStmt.setString(3,coachType);
myRes = myStmt.executeQuery();
while(myRes.next()){
trainArr.add(new Train(
myRes.getInt("train_number"),
myRes.getString("train_name"),
myRes.getString("source"),
myRes.getString("destination"),
myRes.getInt("ac1"),
myRes.getInt("ac2"),
myRes.getInt("ac3"),
myRes.getInt("sleeper"),
myRes.getInt("seater")));
}
return trainArr;
}catch(Exception exc){
exc.printStackTrace();
return null;
}
}
}
coachType=ac1,ac2,ac3,sleeper,seater;
In my opinion there are two approaches:
make a SELECT without the condition on the parametrized column (derived from coachType) and filter the results in your Java code. You'll get all the trains with the desired source and destination, ordered by train_number
myStmt = myConn.prepareStatement("SELECT * from train WHERE source = ? AND destination = ? ORDER BY train_number");
myStmt.setString(1,source);
myStmt.setString(2,destination);
myRes = myStmt.executeQuery();
and then, in your loop on the resultset,
while(myRes.next()) {
if (myRes.getInt(coachType) != 0) { // excludes records with column derived from coachType != 0
trainArr.add(new Train(
myRes.getInt("train_number"),
myRes.getString("train_name"),
myRes.getString("source"),
myRes.getString("destination"),
myRes.getInt("ac1"),
myRes.getInt("ac2"),
myRes.getInt("ac3"),
myRes.getInt("sleeper"),
myRes.getInt("seater")));
}
}
To avoid SQL Injection you can make an apparently useless query:
firstStmt = myConn.prepareStatement("SELECT column_name FROM information_schema.columns WHERE table_schema = ? AND table_name = ? AND column_name = ?");
firstStmt.setString(1, table_schema); // a variable with your schema name
firstStmt.setString(2, "train");
firstStmt.setString(3, coachType);
safeQueryRes = firstStmt.executeQuery();
safeCoachTypeColName = safeQueryRes.next().getString("column_name");
In this way you can use concatenation in your final query, avoiding SQL Injection, as if someone puts some hacking string in your coachType input variable, the first query will fail and nothing dangerous will happen. Otherwise, if the first query will be executed without a SQL Injection, you'll get the "real-and-SQL-Injection-safe" column name that you can use to create your final query with string concatenation:
myStmt = myConn.prepareStatement("SELECT * from train WHERE source = ? AND destination = ? AND " + safeCoachTypeColName + " != 0 ORDER BY train_number");
myStmt.setString(1,source);
myStmt.setString(2,destination);
myRes = myStmt.executeQuery();

Does Flash have a method that does the reverse of toString?

When using ObjectUtil there is a method called, toString() that takes an object. If you pass it a class named, "Person" it will return the string "[class Person]".
var person:Person = new Person();
trace(ObjectUtil.toString(person));//UPDATE I'm not using ObjectUtil.toString()
// traces [class Person]
Is there a toObject() method? Something that takes the same format toString outputs and creates an instance like so:
var person:Person = ObjectUtil.toObject("[class Person]");
UPDATE:
Sorry. This is incorrect. I thought I was using ObjectUtil.toString(). I was not. When I use that method it returns something like:
(com.printui.assets.skins::fontList)#0
accessibilityDescription = ""
accessibilityEnabled = true
accessibilityImplementation = (null)
In my code somewhere it is returning "[class Person]" like I was described. This is the line:
var currentValue:* = target[property];
popUpValueInput.text = currentValue;
I thought it was using instance.toString() but toString() is not returning anything close to that:
var f:fontList = new fontList();
var f1:fontList = new fontList();
trace("" + f);
trace("" + f1);
trace(f1.toString());
Results in:
fontList2
fontList5
fontList5
In general you should do this:
In your Person class add this method:
public function toString():String
{
return "Person" ;
}
So to make an instance of the class by name use this code:
var p = new (getDefinitionByName( ObjectUtils.toString(person)))
or it can be used a regex in general for all classes (thanks to 1.21 gigawatts ):
var p = new (getDefinitionByName( ObjectUtil.toString(Person).match(/\((.*)\)/)[1] ) );

How to make a flattening function for linq object (Linq To Entities for mysql)?

I am upgrading an old program and using linq for basic select, so that I can learn linq in process.
I have a repetitive task of just showing data from various join is grid view,
Below is a sample
protected void Page_Load(object sender, EventArgs e)
{
using ( vavestockModel.vavestockEntities db = new vavestockModel.vavestockEntities())
{
var prod = (from p in db.products select p);
var prodd = (from p in db.productdetails select p);
var prode = (from p in db.product_extra_data select p);
var join1 = (from p in prod
join
pd in prodd
on p.PrStyle equals pd.StyleCode
select new {pr=p,prd=pd }).ToList();
var join2 = (from p in prod
join
pd in prodd
on p.PrStyle equals pd.StyleCode
select new { p.PrShow,p.PrPrice,pd.I_EAN_51,pd.I_EAN_50 }).ToList();
var join3 = (from p in prod
join
pd in prodd
on p.PrStyle equals pd.StyleCode
select new { flattenmodel(p),flattenmodel(pd) }).ToList();
Response.Write(join1);
GridView1.DataSource = join1;
GridView1.DataBind();
}
}
??object flattenmodel(vavestockModel.vavestockEntities en)//? here i want to pass any table row
{
string[] toren;
//foreach (var item in en.)
//{
//}
return toren;
}
join2 can be bound to gridview while join1 cant because they return entity object. Since I have to select all columns writing all names repetitively for so many tables is not a wise choice hence i want to write a function that returns the flattened data.
But I am finding it difficult to proceed. My difficulty is coming because I don't know what should be returned and passed as parameters in this case. Can some one point me in right direction to proceed?
You can get all properties of an object through System.Reflection here is an example how you can get values from all properties and store them in List and than you can assign this List as datasource.
Entities ent = new Entities();
var data = (from x in ent.Employees where x.Id == 1 select x).FirstOrDefault();
List<string> list = ConvertToList(data);
public List<string> ConvertToList(object obj)
{
List<string> list = new List<string>();
PropertyInfo[] pinfo = obj.GetType().GetProperties();
foreach (var info in pinfo)
{
if (info.GetValue(obj) != null)
list.Add(info.GetValue(obj, null).ToString());
}
return list;
}
Edit
You can also return a DataTablefrom anonymous object.
public DataTable ConvertToDataTable(IEnumerable<dynamic> obj)
{
DataTable dt = new DataTable();
foreach (dynamic item in obj)
{
DataRow dr = dt.NewRow();
PropertyInfo[] pinfo = item.GetType().GetProperties();
foreach (var info in pinfo)
{
if (!dt.Columns.Contains(info.Name))
dt.Columns.Add(info.Name, info.PropertyType);
var val= info.GetValue(item, null);
dr[info.Name] = val;
}
dt.Rows.Add(dr);
}
return dt;
}
Here is how you will call it
Entities ent = new Entities();
var data = (from x in ent.Employees where x.Id == 1 select x).FirstOrDefault();
DataTable dt=ConvertToDataTable(data);

Use DatabaseFactory to call Stored Procedure by passing TableType

I am getting error while trying to pass table type to Stored Procedure
Created datatable dataTableMyData with datatype name
DataTable dataTableMyData = new DataTable("myTableType");
My code :
DatabaseFactory.ClearDatabaseProviderFactory();
DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory());
Database db = DatabaseFactory.CreateDatabase();
DataSet dsResult = new DataSet();
DbCommand cmd = db.GetStoredProcCommand("sp_Save_Clue_Auto_Response"))
db.AddInParameter(cmd, "#myTableType", System.Data.SqlDbType.Structured, dataTableMyData);
How to use the DatabaseFactory to pass table type. Please help me here.
Your code doesn't execute the command? Did you forget to post that in your sample?
DataTable table = new DataTable();
SqlDatabase database = (SqlDatabase) DatabaseFactory.CreateDatabase("DatabaseName");
DbCommand dbCommand = database.GetStoredProcCommand("[StoredProcName]");
database.AddInParameter(dbCommand, "#MyTvp",SqlDbType.Structured, table);
DataSet dataSet = database.ExecuteDataSet(dbCommand);
maybe not the best approach, but works:
-- Create the data type
CREATE TYPE dbo.PlantList AS TABLE
(
plant_code char(1) Not Null Primary Key
)
GO
extension method
public static class DatabaseExtensions
{
public static void AddTableTypeParameter<T>(this Database database, DbCommand command, string name, string sqlType, IEnumerable<T> values)
{
var table = new DataTable();
PropertyInfo[] members = values.First().GetType().GetProperties();
foreach (var member in members)
{
table.Columns.Add(member.Name, member.PropertyType);
}
foreach (var value in values)
{
var row = table.NewRow();
row.ItemArray = members.Select(m => m.GetValue(value)).ToArray();
table.Rows.Add(row);
}
var parameter = new SqlParameter(name, SqlDbType.Structured)
{
TypeName = sqlType,
SqlValue = table
};
command.Parameters.Add(parameter);
}
}
and call as follows:
database.AddTableTypeParameter(command, "#region_filter_plants", "dbo.PlantList", regionFilterPlants.Select(p => new { plant_code = p }));

Is it possible to convert JPQL query result with multiple Object types into JSON format automatically?

I'm using JPA Toplink, JAX-RS, NetBean6.9
So far I successfully convert JPQL query result which is List with one object type into JSON.
Following works fine, it generates JSON by the time it gets to the browser because I specified so in JPA annotation
JPA snippet
#XmlRootElement //here enable JSON
#Entity
#Table(name = "MasatosanTest")
Resource class snippet
#Path("allJoin")
#GET
#Produces("application/json")
public List<MasatosanTest> getAllJoinResult() {
EntityManager em = null;
List<Object[]> mt = null;
try {
em = EmProvider.getDefaultManager();
Query query = em.createQuery("SELECT m1, m2 FROM MasatosanTest m1, MasatosanTest2 m2");
mt = query.getResultList();
}
catch(Exception e) {
System.out.println("MasatosanTestResource.java - getJoinedResult ERROR: " + e);
}
finally {
if(em != null) {
em.close();
}
}
//I'm only adding MasatosanTest object into the List not MasatosanTest2
List<MasatosanTest> list = new ArrayList<MasatosanTest>();
MasatosanTest mt = null;
for(Object[] items : mt) {
mt = (MasatosanTest)items[0];
list.add(mt);
}
return list;
}//end getAllJoinResult()
The code above in the browser will output something like:
[MasatosanTest : [[name:foo], [name:moo]]
My problem is when 2 different Object types are returned by JPQL, it won't convert to JSON automatically anymore.
If I modify the code above a bit where I'm adding MasatosanTest2 to list as well.
//Now I'm adding MasatosanTest2 to the List in addition to MasatosanTest
//So changing List data type to List<Object> to accept both object types.
List<Object> list = new ArrayList<Object>();
MasatosanTest mt = null;
MasatosanTest2 mt2 = null;
for(Object[] items : mt) {
mt = (MasatosanTest)items[0];
mt2 = (MasatosanTest2)items[1];
list.add(mt);
list.add(mt2)
}
return list;
Then of course change method return type to List too.
public List<Object> getAllJoinResult() {
Then I get an error complaining it can't be JSON :(
A message body writer for Java type, class java.util.ArrayList,
and MIME media type, application/json, was not found
Is it allowed to have JSON that contains multiple types?
My goal is to have JSON like:
[[MasatosanTest : [[name:foo], [name:moo]],
[MasatosanTest2 : [[name:boo], [name:www]] ]
After testing few based on other people's help online, I figured that returned query result is List<Object[]> and this Object[] is actually Vector.
So, below would do the job (not sure if efficient enough though)
Also JSONStringer is from jettison api.
List<Object[]> out = null;
JSONStringer jstr = null;
sb = new StringBuilder();
Vector vec = null;
for(Object item : out) {
vec = (Vector)item;
jstr = new JSONStringer();
String json = jstr.object()
.key("columnName1").value( vec.get(0) )
.key("columnName2").value( vec.get(1) )
.key("columnName3").value( vec.get(2) )
.key("columnName4").value( vec.get(3) )
.key("columnName5").value( vec.get(4) )
.endObject().toString();
sb.append(json).append(",");
jstr = null;//reset
}
sb.deleteCharAt(sb.length() - 1);
System.out.println("====== json out result =====> " + sb.toString());