Search code examples
c#jsonelasticsearchjson.netjson-query

Query Json how to get elements inside must array


I want to get elements on must array.I have query like this

{
  "from": 0,
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "analyze_wildcard": true,
            "default_operator": "and",
            "fields": [
              "entityStatusDesc"
            ],
            "query": "$entityStatusDesc"
          }
        },
        {
          "query_string": {
            "analyze_wildcard": true,
            "default_operator": "and",
            "fields": [
              "instanceStartTime"
            ],
            "query": "$instanceStartTime"
          }
        },
        {
          "match": {
            "model": {
              "query": "instance"
            }
          }
        }
      ]
    }
  },
  "size": 10,
}

I tried ;

JObject o = JObject.Parse(json);

        var acme = o.SelectToken("$.must[0]");

But didn't work.I want to get this part of json with must[0] call;

{
          "query_string": {
            "analyze_wildcard": true,
            "default_operator": "and",
            "fields": [
              "entityStatusDesc"
            ],
            "query": "$entityStatusDesc"
          }
        },

Is there any different way for getting must areas?I will be grateful if anyone could help me.Thanks.


Solution

  • One of the options is to provide full path:

    var acme = o.SelectToken("$.query.bool.must[0]");
    

    or just use indexers:

    var acme = o["query"]["bool"]["must"][0];