using System.Collections.Generic;
public static void Main()
var source = new List<TreeModel>
ChangeFlowsFromParent = false,
ChangeFlowsToParent = true,
StreamName = "ArgOS_2_0",
ChangeFlowsFromParent = true,
ChangeFlowsToParent = true,
StreamName = "ArgOS_2_0_DHAL1",
ChangeFlowsFromParent = false,
ChangeFlowsToParent = true,
StreamName = "ArgOS_2_0_child_DHAL2",
Parent = "ArgOS_2_0_DHAL1",
ChangeFlowsFromParent = false,
ChangeFlowsToParent = true,
StreamName = "ArgOS_child_Gen2",
Parent = "ArgOS_2_0_child_DHAL2",
ChangeFlowsFromParent = true,
ChangeFlowsToParent = true,
StreamName = "ArgOS_2_0_DHAL2",
ChangeFlowsFromParent = true,
ChangeFlowsToParent = true,
StreamName = "ArgOS_2_0_DHAL3",
var output = MapToTreeModelJsonCollection(source);
var json = JsonConvert.SerializeObject(output,Formatting.Indented);
static ICollection<TreeModelJson> MapToTreeModelJsonCollection(ICollection<TreeModel> source)
var allItems = source.Select(e => new TreeModelJson
ChangeFlowsFromParent = e.ChangeFlowsFromParent.ToString().ToLower(),
ChangeFlowsToParent = e.ChangeFlowsToParent.ToString().ToLower(),
Compliance = e.Compliance,
Parent = e.Parent ?? "none",
StreamName = e.StreamName,
StreamType = e.StreamType,
foreach (var item in allItems)
var children = allItems.Where(e => e.Parent == item.StreamName).ToList();
item.Children = children;
return allItems.Where(e => e.Parent == "none").ToList();
public string StreamName { get; set; }
public string ParentName { get; set; }
public string StreamType { get; set; }
public bool ChangeFlowsFromParent { get; set; }
public bool ChangeFlowsToParent { get; set; }
public string Compliance { get; set; }
public string Parent { get; set; }
[JsonProperty("ChangeFlowsFromParent")]
public string ChangeFlowsFromParent { get; set; }
[JsonProperty("ChangeFlowsToParent")]
public string ChangeFlowsToParent { get; set; }
[JsonProperty("StreamType")]
public string StreamType { get; set; }
[JsonProperty("streamName")]
public string StreamName { get; set; }
public string Parent { get; set; }
[JsonProperty("Compliance")]
public string Compliance { get; set; }
[JsonProperty("children", NullValueHandling = NullValueHandling.Ignore)]
public ICollection<TreeModelJson> Children { get; set; }