2
{
   "kind": "folderTree",
   "data":
   [
       {
           "id": "IEAAALNZI7777777",
           "title": "Root",
           "childIds":
           [
               "IEAAALNZI4ADAKBQ",
               "IEAAALNZI4ADAMBQ",
               "IEAAALNZI4ADAMBR"
           ],
           "scope": "WsRoot"
       },
       {
           "id": "IEAAANE7I7777777",
           "title": "Root",
           "childIds":
           [
               "IEAAANE7I4AC2NTX"
           ],
           "scope": "WsRoot"
       },
       {
           "id": "IEAAALNZI7777776",
           "title": "Recycle Bin",
           "childIds":
           [
               "IEAAALNZI4ADALZ2",
               "IEAAALNZI4ADAL52",
               "IEAAALNZI4ADALR3"
           ],
           "scope": "RbRoot"
       }
   ]
}

Im trying to query the following json structure, searching the child items I want to return the id for a given title.

I am trying something like this:

var folder = json["data"].Children().Where(x => x["Title"] == "Root");

But I'm not sure of the correct syntax

3
  • 3
    Why not deserialise it and use normal Linq? Commented Nov 17, 2015 at 10:51
  • Thanks, can I de-serialise into an anonymous object? If so how do I query this. Commented Nov 17, 2015 at 11:24
  • The title suggests this was already serialised, don't you have the actual classes this came from? Commented Nov 17, 2015 at 11:25

1 Answer 1

2

You can use SelectTokens to query LINQ to JSON objects. It supports JSONPath query syntax including wildcards. You can then further narrow down the search with a Where clause:

var folders = json.SelectTokens("data[*]").Where(t => (string)t["title"] == "Root").ToList();

It also supports filtering of array entries based on property values if you don't want the extra Where clause:

var folders = json.SelectTokens("data[?(@.title == 'Root')]").ToList();

Both of the above do the same thing. Incidentally, you've got two folders whose title is "Root" in your JSON, so your query will return multiple results.

Sign up to request clarification or add additional context in comments.

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.