Here's what I described in the comments. First, you need some metadata. I use an enum here (since it makes typos less likely - though it does make the code ever so slightly more complicated.
(UPDATE: I found a bug, I was treating everything as strings. now I treat the leaf nodes of the trees as object
)
First the category types:
private enum CategoryName
{
Main,
Interior,
Exterior,
}
and the category metadata:
private static readonly Dictionary<string, CategoryName> _categoryMetadata =
new Dictionary<string, CategoryName>
{
{"id", CategoryName.Main},
{"desc", CategoryName.Main},
{"system", CategoryName.Main},
{"bedrooms", CategoryName.Interior},
{"flooring", CategoryName.Interior},
{"roof", CategoryName.Exterior}
};
Now, your input JSON. Note that I single-quoted the strings, it makes it easier to use in C# and doesn't change the nature of the JSON. I also took out all your ...
entries:
private const string InputJson = @"
{
'id': 1,
'desc': 'foo',
'system': 'axmls',
'bedrooms': 5,
'flooring': 'tile',
'roof': 'tesla',
}";
Finally the code. As I described in the comments "Build a metadata dictionary (string, string) that maps each item (bedrooms, flooring, roof) to categories (main, interior, exterior). Read the data you have (the big initial list) into a Dictionary<string, string>. Then walk that Dictionary, looking up the category in the metadata. Finally populate a Dictionary<string, Dictionary<string, string>> with the results and serialize it back out to JSON":
var output = new Dictionary<string, Dictionary<string, object>>();
var input = JsonConvert.DeserializeObject<Dictionary<string, object>>(InputJson);
foreach (var item in input)
{
if (_categoryMetadata.TryGetValue(item.Key, out var category))
{
var categoryString = category.ToString().ToLower();
if (!output.ContainsKey(categoryString))
{
output[categoryString] = new Dictionary<string, object>();
}
output[categoryString].Add(item.Key, item.Value);
}
}
var result = JsonConvert.SerializeObject(output, Formatting.Indented);
When that's finished (and after my update), the JSON looks like:
{
"main": {
"id": 1,
"desc": "foo",
"system": "axmls"
},
"interior": {
"bedrooms": 5,
"flooring": "tile"
},
"exterior": {
"roof": "tesla"
}
}
In real life, you'd remove the Formatting.Indented
parameter on the JsonConvert.Serialize
call.