Selecting Simple Properties from a Hierarchical JSON Part II

I thought this question might be poorly formed, so I created a "sister" question, which is much larger than the point that the specific result is mentioned about. Please see: Request JSON arrays with Linq, JSON.NET, C # If this question is answered before, I will try to answer this question myself using the information from another question ... :) Thank you!

In the previous question ( Choosing Simple Properties from Hierarchical JSON ) I asked how to select simple properties from Hierarchical JSON. The answer there [inserted as a Linq request at the end of this post] is extremely useful (and, after posting this, I studied Linq and JSON.NET a bit). Therefore, I do not use this forum because I am lazy - I use it when I am really stuck and cannot find the answers in books that I have access to.

I do not understand how to continue the example presented by my previous question. This question is based on the previous one, so here (as clearly as possible) I would like to learn how to do Linq in one query.

Recall: I work with dynamic JSON like this (it is more complex than the JSON introduced in my previous part I, because it contains arrays):

{
    "Branch1": {
        "Prop1A": "1A",
        "Prop1B": "1B",
        "Prop1C": "1C",
        "Branch2": {
            "Prop2A": "2A",
            "Prop2B": "2B",
            "Prop2C": "2C",
            "Branch3": [{
                "Prop3A": "3A",
                "Prop3B": "3B",
                "Prop3C": "3C"
            }, {
                "Prop3D": "3D",
                "Prop3E": "3E",
                "Prop3F": "3F"
            }, {
                "Prop3G": "3G",
                "Prop3H": "3H",
                "Prop3I": "3I"
            }]
        },
        "Branch4": [{
            "Prop4A": "4A",
            "Prop4B": "4B",
            "Prop4C": "4C"
        }, {
            "Prop4E": "4E",
            "Prop4F": "4F",
            "Prop4G": "4G"
        }, {
            "Prop4H": "4H",
            "Prop4I": "4I",
            "Prop4I": "4I"
        }]
    }
}

, JSON , JSON, JSON JSON.

, JSON List, #. List, JSON , .

List JObject; "" , , JObject JSON ( , "parent" ). [ "" , ... JArray JSON...]

, , . , Branch1 Prop1A, 1B 1C. , , [0] :

{"Prop1A":"1A","Prop1B":"1B","Prop1C":"1C", Parent:""}

, , [2] Branch2:

{"Prop2A":"2A","Prop2B":"2B","Prop2C":"2C", Parent:"Branch1"}

, [2] Branch3, Branch3 , , [2]:

[
 {"Prop3A": "3A","Prop3B": "3B","Prop3C": "3C"},
 {"Prop3D": "3D","Prop3E": "3E","Prop3F": "3F"},
 {"Prop3G": "3G","Prop3H": "3H","Prop3I": "3I"}
]

, ""... [2], . ( dbc- "Parent" JObject, , Parent ):

[{"Prop3A": "3A","Prop3B": "3B","Prop3C": "3C","Parent":"Branch2"},
 {"Prop3D": "3D","Prop3E": "3E","Prop3F": "3F","Parent":"Branch2"},
 {"Prop3G": "3G","Prop3H": "3H","Prop3I": "3I","Parent":"Branch2"}
]

, : * , - JSON, , JObject , . * , JSON , JObject , .

, , , "if myJsonObject is JArray" Linq , , JArray. , - ?: , , .

:

var query3 = from o in root.DescendantsAndSelf().OfType<JObject>()      // Find objects
             let l = o.Properties().Where(p => p.Value is JValue)       // Select their primitive properties
             where l.Any()                                              // Skip objects with no properties
             // Add synthetic "Parent" property
             let l2 = l.Concat(new[] { new JProperty("Parent", o.Ancestors().OfType<JProperty>().Skip(1).Select(a => a.Name).FirstOrDefault() ?? "") })
             select new JObject(l2);                                    // And return a JObject.

var list3 = query3.ToList();

, .

, !

0
1
0

Source: https://habr.com/ru/post/1648872/


All Articles