0

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! :)

2
  • if you don't have a concrete type for your JSON data then just deserialize this into a dynamic object and then check for control count on that object as explained siddharthpandey.net/… OR you can also create a anonymous type that just cares about members you need as explained here siddharthpandey.net/… Commented Mar 6, 2017 at 16:52
  • Something like this? Querying JSON with JSONPath. Commented Mar 6, 2017 at 16:58

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

You might consider using .Any() rather than .Count() - count will force an iteration of the entire collection, Any will return as soon as it finds the first.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.