Hi.
C# Issue a head:
I have an
anonymous Json.
I need to disassemble only the internal objects (those surrounded by curly braces) and flatten them.
This should be done in a generic way so that no matter what the depth of the object (a child can contain an infinity of children and grandchildren) - all objects that are in curly brackets (and only they) will be flattened out into the Dictionary.
Please look at the following example:
{
"A": "xxxxx",
"Groups": [
"xxxxx",
"xxxxx",
"xxxxx"
],
"B": "xxxxx",
"C": "xxxxx",
"D": "xxxxx",
"E": "xxxxx",
"F": "xxxxx",
"G": null,
"H": "xxxxx",
"I": {
"H": "xxxxx"
},
"J": {
"K": "xxxxx",
"L": "xxxxx",
"M": "xxxxx",
"N": "xxxxx",
"O": {
"P": "xxxxx",
"Q": "xxxxx",
"R": "xxxxx",
"T": "xxxxx",
"U": {
"V": "xxxxx"
}
},
"W": "xxxxx"
},
"X": {
"Y": "xxxxx",
"Z": "xxxxx"
},
"AA": {
"AB": "xxxxx",
"AC": "xxxxx"
},
"AD": {
"AE": "xxxxx",
"AF": "xxxxx"
}
}
I need the result to be:
"H": "xxxxx"
"K": "xxxxx",
"L": "xxxxx",
"M": "xxxxx",
"N": "xxxxx",
"P": "xxxxx",
"Q": "xxxxx",
"R": "xxxxx",
"T": "xxxxx",
"V": "xxxxx"
"W": "xxxxx"
"Y": "xxxxx",
"Z": "xxxxx"
"AB": "xxxxx",
"AC": "xxxxx"
"AE": "xxxxx",
"AF": "xxxxx"
Please note also the second object in Json ('Groups'): it should
not be part of the flattened list.
What I have tried:
I tried using SelectTokens like this:
var allLevels = schemaObject.SelectTokens("$..*").Where(t => !t.HasValues).ToDictionary(t => t.Path, t => t.ToString());
but it also breaks down the most external parameters (for example: "A") and the "Groups" into three objects (when in fact I do not want it at all).