Hefin Jones
Hefin Jones

Reputation: 29

Dynamic JSON querying JSON.NET

I have this piece of JSON:

{ 
    "grid": 4, 
    "rows": [ 
        { 
            "label": "Full Width", 
            "name": "FullWidth", 
            "areas": [ 
                { 
                    "grid": 16, 
                    "hasConfig": false, 
                    "controls": [ 
                        { 
                            "value": "Test", 
                            "editor": { 
                                "name": "Headline", 
                                "alias": "headline", 
                                "view": "textstring", 
                                "render": null, 
                                "icon": "icon-coin", 
                                "config": { 
                                    "style": "font-size: 36px; line-height: 45px; font-weight: bold", 
                                    "markup": "<h1>#value#</h1>" 
                                } 
                            }, 
                            "active": false 
                        }, 
                        { 
                            "value": "Test", 
                            "editor": { 
                                "name": "Headline", 
                                "alias": "headline", 
                                "view": "textstring", 
                                "render": null, 
                                "icon": "icon-coin", 
                                "config": { 
                                    "style": "font-size: 36px; line-height: 45px; font-weight: bold", 
                                    "markup": "<h1>#value#</h1>" 
                                    } 
                            }, 
                            "active": false 
                        } 
                    ], 
                    "active": false 
                } 
            ], 
            "hasConfig": false, 
            "id": "e0f6ab79-f562-b86b-f32b-d5520173f0cb", 
            "active": false 
        } 
    ] 
}

I want to know if any rows (could be multiple rows) have any Controls (Count > 0). I don't need to know what's contained within any of the fields, just if any rows have a control count of > 0 - so just true or false. I've been trying to achieve this using a LINQ query but not getting anywhere fast!

This JSON is dynamic, so I can't deserialize it. Any help would be greatly appreciated! :)

Upvotes: 0

Views: 773

Answers (2)

Hefin Jones
Hefin Jones

Reputation: 29

Ended up using the link given by @dbc - this is my code:

JObject sideBar = JObject.Parse(sidebarContentCol.ToString());

IEnumerable<JToken> sidebarControls = sideBar.SelectTokens("$.rows[*].areas[*].controls[*]");

var controls = (bool)(sidebarControls.Count() == 0);

Upvotes: 2

Dan Field
Dan Field

Reputation: 21641

You can do it like this:

string json = @"{ 
    'grid': 4, 
    'rows': [ 
        { 
            'label': 'Full Width', 
            'name': 'FullWidth', 
            'areas': [ 
                { 
                    'grid': 16, 
                    'hasConfig': false, 
                     'controls': [ 
                        { 
                            'value': 'Test', 
                            'editor': { 
                                'name': 'Headline', 
                                'alias': 'headline', 
                                'view': 'textstring', 
                                'render': null, 
                                'icon': 'icon-coin', 
                                'config': { 
                                    'style': 'font-size: 36px; line-height: 45px; font-weight: bold', 
                                    'markup': '<h1>#value#</h1>' 
                                } 
                            }, 
                            'active': false 
                        }, 
                        { 
                            'value': 'Test', 
                            'editor': { 
                                'name': 'Headline', 
                                'alias': 'headline', 
                                'view': 'textstring', 
                                'render': null, 
                                'icon': 'icon-coin', 
                                'config': { 
                                    'style': 'font-size: 36px; line-height: 45px; font-weight: bold', 
                                    'markup': '<h1>#value#</h1>' 
                                    } 
                            }, 
                            'active': false 
                        } 
                    ],
                    'active': false 
                } 
            ], 
            'hasConfig': false, 
            'id': 'e0f6ab79-f562-b86b-f32b-d5520173f0cb', 
            'active': false 
        } 
    ] 
}";

var jobject = JObject.Parse(json);

var test = jobject
    .Descendants()
    .Where(t => t.Type == JTokenType.Property && ((JProperty)t).Name == "controls" 
            && t.Ancestors()
                .Any(a => a.Type == JTokenType.Property && ((JProperty)a).Name == "rows"))
    .Any();

Parse it into a JObject, look for a Descendant named "controls" that has a parent (ancestor) named "rows". test will come out as true if so, false if not.

Upvotes: 0

Related Questions