Issue191

CSharp · Console

/Program.cs

using System;
using ChoETL;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

public class Program
{
	public static void Main()
	{
		typeof(ChoJSONReader).GetAssemblyVersion().Print();
		"".Print();
		
		string json = @"{
  ""BrandId"": ""998877665544332211"",
  ""Categories"": [ ""112233445566778899"" ],
  ""Contact"": {
    ""Phone"": [
      {
        ""Value"": ""12346789"",
        ""Description"": { ""vi"": ""Phone"" },
        ""Type"": 1
      },
      {
        ""Value"": ""987654321"",
        ""Description"": { ""vi"": ""Phone"" },
        ""Type"": 1
      }
    ]
  }
}";
		ChoETLSettings.KeySeparator = '-';
		using (var r = ChoJSONReader.LoadText(json)
			   .WithField("BrandId")
			   .WithField("Category", jsonPath: "Categories[0]", isArray: false)
			   .WithField("Phone", jsonPath: "Contact.Phone[*]")
			  )
		{
			var dt = r.SelectMany(rec => ((IList)rec.Phone).OfType<dynamic>().Select(rec1 => 
																			{
																				dynamic ret = new ChoDynamicObject();
																				ret["BrandId"] = rec.BrandId;
																				ret["Contact.Phone.Value"] = rec1.Value;
																				ret["Contact.Phone.Description.vi"] = rec1.Description.vi;
																				ret["Contact.Phone.Type"] = rec1.Type;
																				ret["Category"] = rec.Category;
																				return ret;
																			})).AsDataTable();
			
			dt.DumpAsJson().Print();
		}
	}
}
;