using System.Collections.Generic;
using Newtonsoft.Json.Linq;
public static void Main()
var relationships = GetRelationships();
dynamic relationship = new JObject();
relationship.name = relationships[0].Industries[1].Name;
relationship.children = new JArray();
var lftIndustries = relationships[0].Industries.Where(k => k.Key > 1);
JToken currentContainer = null;
var lastOne = lftIndustries.Count();
foreach (var item in lftIndustries)
var industry = new JObject();
if (currentContainer != null)
var node = ((JContainer)currentContainer).Last;
var childArray = (JArray)((JContainer) node).Last;
industry = new JObject(new JProperty("name", item.Value.Name));
industry = new JObject(new JProperty("name", item.Value.Name), new JProperty("children", new JArray()));
childArray.Add(industry);
currentContainer = industry;
var node = ((JContainer)relationship).Last;
var childArray = (JArray)((JContainer) node).Last;
industry = new JObject(new JProperty("name", item.Value.Name));
industry = new JObject(new JProperty("name", item.Value.Name), new JProperty("children", new JArray()));
childArray.Add(industry);
currentContainer = industry;
json = relationship.ToString();
private static List<Relationship> GetRelationships(){
var naicsCodes = new List<NaicsCode>();
naicsCodes.Add(new NaicsCode{Code = "XX", Name = "Level XX", Path = null, PathName = "/3/", Level = 1, IndustryId = "DCA7B69E-0A01-E711-BAE1-D7C01D651811"});
naicsCodes.Add(new NaicsCode{Code = "XXX", Name = "Level XXX", Path = null, PathName = "/3/17/", Level = 2, IndustryId = "012E0591-3F01-E711-BAE1-D7C01D651811"});
naicsCodes.Add(new NaicsCode{Code = "XXXX", Name = "Level XXXX", Path = null, PathName = "/3/17/2", Level = 3, IndustryId = "0146576C-5501-E711-BAE1-D7C01D651811"});
naicsCodes.Add(new NaicsCode{Code = "XXXXX", Name = "Level XXXXX", Path = null, PathName = "/3/17/2/2/2/", Level = 4, IndustryId = "314E09F4-5901-E711-BAE1-D7C01D651811"});
naicsCodes.Add(new NaicsCode{Code = "XXXXXX", Name = "Level XXXXXX", Path = null, PathName = "/3/17/2/2/1/", Level = 5, IndustryId = "D06C3B31-5B01-E711-BAE1-D7C01D651811"});
var relationships = new List<Relationship>{new Relationship{ClientId = 99, Name = "Apple"}};
group code by code.Level into levelGroup
foreach (var levelGroup in groupedCodes)
relationships[0].Industries.Add(levelGroup.Key, new Industry());
foreach (var code in levelGroup)
var indtry = relationships[0].Industries[levelGroup.Key];
if (string.IsNullOrEmpty(indtry.Name))
indtry.Name = string.Format("{0}-{1}", code.Code, code.Name);
indtry.NaicsCodes.Add(new NaicsCode{Code = code.Code, Name = code.Name, Path = code.Path, PathName = code.PathName, Level = code.Level, IndustryId = code.IndustryId});
public string Code {get;set;}
public string Name {get;set;}
public object Path {get;set;}
public string PathName {get;set;}
public int Level {get;set;}
public string IndustryId {get;set;}
NaicsCodes = new List<NaicsCode>();
public string Name {get;set; }
public List<NaicsCode> NaicsCodes {get;set;}
public class Relationship
Industries = new Dictionary<int, Industry>();
public int ClientId {get;set;}
public string Name {get;set;}
public Dictionary<int, Industry> Industries{get;set;}