using System.Collections.Generic;
public static void Main()
var list = new List<string>{"example.string.1", "example.string.2", "example.anotherstring.1", "example.anotherstring.2", "System", "System.Diagnostics", "System.Text", "System.Collections.Generic", "System.Linq", "System.Diagnostics.1", "System.Text.1", "System.Collections.Generic.1", "System.Linq.1", "System.Diagnostics.2", "System.Text.2", "System.Collections.Generic.2", "System.Linq.2"};
var groups = list.Select(x => x.Split('.')).GroupBy(x => x.FirstOrDefault()).Select(x => new RawValue(x.Key, x.Select(y => y.Skip(1))));
var settings = new JsonSerializerSettings{NullValueHandling = NullValueHandling.Ignore};
var result = GroupNameSpaces(groups);
var json = JsonConvert.SerializeObject(result, Formatting.Indented, settings);
private static IEnumerable<RawValue> GetRawValues(RawValue value)
return value.Values.Where(x => x.Any()).GroupBy(x => x.FirstOrDefault()).Select(x => new RawValue(x.Key, x.Select(y => y.Skip(1))));
private static NameSpace GroupNameSpaces(IEnumerable<RawValue> groups)
var result = new NameSpace();
foreach (var group in groups)
result.Add(group.Key, GroupNameSpaces(GetRawValues(group)));
public RawValue(string key, IEnumerable<IEnumerable<string>> values)
public IEnumerable<IEnumerable<string>> Values
public class NameSpace : Dictionary<string, NameSpace>