using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Data.DataSetExtensions;
using System.Text.RegularExpressions;
public Field(string Path, string Type, bool isArray = false)
this.Name = Path.Substring(Path.LastIndexOf('.') + 1);
this.FieldName = this.Path.Substring(0, 1) == "." ? this.Path.Substring(1) : this.Path;
string[] str = this.FieldName.Split('.');
this.isRoot = str.Length == 1;
this.TableName = "llodiroot";
this.TableName = this.Name;
this.TableName = str[str.Length - 2];
public bool isUltimateChildren;
public string ParentTable;
public Table(DataTable dt)
this.isRoot = dt.ParentRelations.Count == 0;
this.isUltimateChildren = dt.ChildRelations.Count == 0;
this.TableName = dt.TableName;
this.ParentTable = this.isRoot ? null : dt.ParentRelations[0].ParentTable.TableName;
this.ParentIndex = this.isRoot ? 0 : dt.DataSet.Tables.IndexOf(dt.ParentRelations[0].ParentTable);
this.PK = this.isUltimateChildren ? null : dt.PrimaryKey[0].ColumnName;
this.FK = this.isRoot ? null : dt.ParentRelations[0].ChildColumns[0].ColumnName;
List<Field> Fields = new List<Field>();
List<Table> Tables = new List<Table>();
List<List<int>> Rows = new List<List<int>>();
string json = "[{\"nombre\":\"paco\",\"apellido\":\"lopez\",\"edad\":18,\"salario\":0.12,\"soltero\":true,\"familia\":[{\"tipo\":\"abuelos\",\"miembros\":[{\"nombre\":\"antonio\"},{\"nombre\":\"javier\",\"apodo\":\"yayo\"}],\"conyuges\":[\"rocio\",\"maria\"]}],\"aspecto\":{\"pelo\":\"negro\",\"estatura\":1.80,\"peso\":90}},{\"nombre\":\"mario\",\"edad\":81,\"salario\":120.12,\"soltero\":false,\"familia\":[{\"tipo\":\"padres\",\"miembros\":[{\"nombre\":\"piti\"},{\"nombre\":\"encarna\"}]},{\"tipo\":\"hermanos\",\"miembros\":[{\"nombre\":\"miguel\"}]}],\"aspecto\":{\"pelo\":\"escaso\",\"estatura\":1.78,\"peso\":110}}]";
JToken parsed = (JToken)JsonConvert.DeserializeObject(json);
DataSet ds = ToDataSet(json);
DataTable indexes = MakeIndex (ds);
foreach (DataColumn col in indexes.Columns)
Console.Write(col.ColumnName+",");
foreach (DataRow r in indexes.Rows)
foreach (DataColumn c in indexes.Columns) Console.Write(r[c]+",");
public DataTable MakeIndex (DataSet ds)
DataTable initial = ds.Tables["llodiroot"];
for (int i = 1; i < ds.Tables.Count; i++)
string newKey = Tables[i].PK;
var linq = from t1 in initial.AsEnumerable()
join t2 in ds.Tables[i].AsEnumerable()
on t1.Field<int>(Tables[Tables[i].ParentIndex].PK)
equals t2.Field<int>(Tables[i].FK) into ps
from t2 in ps.DefaultIfEmpty()
DataTable replace = initial.Clone();
replace.Constraints.Clear();
replace.Columns.Add(new DataColumn(newKey, typeof(Int32)));
foreach (Field f in Fields.FindAll(x => x.TableName == ds.Tables[i].TableName))
replace.Columns.Add(new DataColumn(f.FieldName));
foreach (dynamic row in linq)
DataRow dr = replace.NewRow();
foreach (DataColumn c in initial.Columns) dr[c.ColumnName] = row.t1[c.ColumnName];
if(!Tables[i].isUltimateChildren) dr[newKey] = (int)row.t2[newKey];
foreach (Field f in Fields.FindAll(x => x.TableName == ds.Tables[i].TableName)) if(row.t2 != null) dr[f.FieldName] = row.t2[f.Name];
public void Test(int test, DataSet ds = null, int IndexTabla = 0, int RowNum = 0, string fieldname = "nombre")
foreach (Field f in Fields) Console.WriteLine(f.FieldName + " (" + f.TableName + " - " + f.isRoot.ToString() + ")");
foreach (Table t in Tables) if (!t.isRoot) Console.WriteLine(t.TableName + " relacionado con " + t.ParentTable + " por " + t.FK);
foreach (DataTable dt in ds.Tables)
Console.WriteLine("\n\n"+dt.TableName+"\n\n");
foreach (DataColumn col in dt.Columns) Console.WriteLine(col.ColumnName);
public void ParseJson(JToken parsedObject)
foreach (dynamic entry in parsedObject)
string type = entry.GetType().ToString().Split('.')[3];
string entryPath = entry.Path == null ? "" : Regex.Replace(entry.Path, @"\[([0-9]+)\]+", "");
foreach (dynamic child in entry.Children())
string childType = child.GetType().ToString().Split('.')[3];
string childPath = child.Path == null ? "" : Regex.Replace(child.Path, @"\[([0-9]+)\]+", "");
if (!Fields.Exists(x => x.Path == childPath))
Fields.Add(new Field(childPath, child.Value.GetType().ToString()));
if (!Fields.Exists(x => x.Path == entryPath))
Fields.Add(new Field(entryPath, entry.Value.GetType().ToString(),true));
public DataSet ToDataSet(string json)
string rootJson = "{\"llodiroot\": " + json + "}";
XmlDocument xml = (XmlDocument)JsonConvert.DeserializeXmlNode(rootJson, "llodiroot");
DataSet ds = new DataSet();
ds.ReadXml(new XmlNodeReader(xml));
public void DeclareTables(DataSet ds)
foreach (DataTable dt in ds.Tables)
Tables.Add(new Table(dt));