# How to GET a Field from nested JSON Data using Query

**URL:** https://discuss.elastic.co/t/how-to-get-a-field-from-nested-json-data-using-query/189812
**Category:** Elasticsearch
**Created:** [July 10, 2019, 3:27pm UTC](https://discuss.elastic.co/t/how-to-get-a-field-from-nested-json-data-using-query/189812 "2019-07-10T15:27:10Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![samyo](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@samyo](https://discuss.elastic.co/u/samyo)
#### Post date: [July 10, 2019, 3:27pm UTC](https://discuss.elastic.co/t/how-to-get-a-field-from-nested-json-data-using-query/189812/1 "2019-07-10T15:27:10Z")

</div>

Hello folks ,  
i have this bellow JSON Data, and i want to write a Query , the Query is ,  
( **Give me all Entities where the "ai\_id" has the Value = 0** ).

the JSON Data ,

```auto
      {
        "_index": "try1",
        "_type": "_doc",
        "_id": "2",
        "_score": 1,
        "_source": {
          "target": {
            "br_id": 0,
            "an_id": 0,
            "ai_id": 0,
            "explanation": [
              "element 1",
              "element 2"
            ]
          },
          "process": {
            "an_id": 1311,
            "pa_name": "micha"
          },
          "text": "hello world"
        }
      },
      {
        "_index": "try1",
        "_type": "_doc",
        "_id": "1",
        "_score": 1,
        "_source": {
          "target": {
            "br_id": 0,
            "an_id": 1,
            "ai_id": 1,
            "explanation": [
              "element 3",
              "element 4"
            ]
          },
          "process": {
            "an_id": 1311,
            "pa_name": "luca"
          },
          "text": "the all People are good"
        }
      }
    ]
  }
}

```

I tried something like this , and did not work ,

```auto
GET try1\_search   
{
  "query":{
        { "match_all": { "ai_id": 0}}
  }
} 

```

Please any Idea or Suggestion , i will be thankfull.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [July 10, 2019, 4:13pm UTC](https://discuss.elastic.co/t/how-to-get-a-field-from-nested-json-data-using-query/189812/2 "2019-07-10T16:13:41Z")

</div>

Try:

```auto
GET try1\_search   
{
  "query":{
        { "match": { "target.ai_id": 0}}
  }
} 

```

---

<div class="post-metadata">

### Author: ![samyo](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@samyo](https://discuss.elastic.co/u/samyo)
#### Post date: [July 11, 2019, 7:49am UTC](https://discuss.elastic.co/t/how-to-get-a-field-from-nested-json-data-using-query/189812/3 "2019-07-11T07:49:13Z")

</div>

thx , it worked fine .

Can we write your Query using the RestClient API in low level and Highlevel client, and how ?  
thx in advance.

---

<div class="post-metadata">

### Author: ![samyo](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@samyo](https://discuss.elastic.co/u/samyo)
#### Post date: [July 11, 2019, 9:07am UTC](https://discuss.elastic.co/t/how-to-get-a-field-from-nested-json-data-using-query/189812/4 "2019-07-11T09:07:29Z")

</div>

I wrote smothing like this , using the RestClient in Low Level Client, and it worked fine as "Match\_all" , but the Above Query is not about Match\_all,

```auto
        try {
            RestClient restClient = RestClient.builder(
                    new HttpHost("localhost", 9200, "http")).build();

            Response response1 = restClient.performRequest("GET", "/try1/_doc/_search?size=" + 10, Collections.singletonMap("pretty", "true"));
            String responseBody = EntityUtils.toString(response1.getEntity());
            System.out.println("result is : " + responseBody);

        } catch (UnknownHostException ex) {
            System.out.println("failure : " + ex.getMessage());
        }

```

Can you please expand my Code , so i can write(translate) the ("match": { "target.ai\_id": 0}) , in it .  
I will be thankfull.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [July 12, 2019, 3:08pm UTC](https://discuss.elastic.co/t/how-to-get-a-field-from-nested-json-data-using-query/189812/5 "2019-07-12T15:08:56Z")

</div>

Here we go with the RestHighLevelClient.

```auto
SearchResponse response = client.search(new SearchRequest("test").source(
        new SearchSourceBuilder().query(
                QueryBuilders.matchQuery("target.ai_id", 0)
        )
), RequestOptions.DEFAULT);

```

---

<div class="post-metadata">

### Author: ![samyo](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@samyo](https://discuss.elastic.co/u/samyo)
#### Post date: [July 15, 2019, 8:34am UTC](https://discuss.elastic.co/t/how-to-get-a-field-from-nested-json-data-using-query/189812/6 "2019-07-15T08:34:33Z")

</div>

thx it worked fine , and i get this good result ,

```auto
"hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [
 {
        "_index": "try1",
        "_type": "_doc",
        "_id": "2",
        "_score": 1,
        "_source": {
          "target": {
            "br_id": 0,
            "an_id": 0,
            "ai_id": 0,
            "explanation": [
              "element 1",
              "element 2"
            ]
          },
          "process": {
            "an_id": 1311,
            "pa_name": "micha"
          },
          "text": "hello world"
        }
      }
]

```

Is there any Method so i can just take the Content of the "hits\_array"?  
i need to get this Result like bellow ,

```auto
 {
 "target": {
            "br_id": 0,
            "an_id": 0,
            "ai_id": 0,
            "explanation": [
              "element 1",
              "element 2"
            ]
          },
          "process": {
            "an_id": 1311,
            "pa_name": "micha"
          },
          "text": "hello world"
        }

```

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [July 15, 2019, 9:43am UTC](https://discuss.elastic.co/t/how-to-get-a-field-from-nested-json-data-using-query/189812/7 "2019-07-15T09:43:25Z")

</div>

You can't get this format exactly I think.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [August 12, 2019, 9:49am UTC](https://discuss.elastic.co/t/how-to-get-a-field-from-nested-json-data-using-query/189812/9 "2019-08-12T09:49:36Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
