# How to fetch first value from Json Array

**URL:** <https://discuss.elastic.co/t/how-to-fetch-first-value-from-json-array/355214>\
**Category:** Logstash\
**Created:** [March 12, 2024, 8:53am UTC](https://discuss.elastic.co/t/how-to-fetch-first-value-from-json-array/355214 "2024-03-12T08:53:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![upreddy](https://avatars.discourse-cdn.com/v4/letter/u/f04885/32.png) [@upreddy](https://discuss.elastic.co/u/upreddy)\
**Post date:** [March 12, 2024, 8:53am UTC](https://discuss.elastic.co/t/how-to-fetch-first-value-from-json-array/355214/1 "2024-03-12T08:53:06Z")

</div>

Hi Team,  
I am sharing logline below.  
"independentFailureReport": {  
"failureReportEntries": [  
{  
"errorCode": abc,  
"additionalInfo": {  
"length": xyz,  
"result": "XXXXXXXXXXXXXXXXXXX"  
},  
"execScheduleInfo": {  
"enableReportTimeStamps": 0,  
"numTriggerSources": ,  
"executionMode": 0  
},  
"creationTime": {  
"utcInMilliseconds": 15245  
},  
"failureCounter": 0  
},  
{  
"errorCode": abc,  
"additionalInfo": {  
"length": xyz,  
"result": "XXXXXXXXXXXXXXXXXXX"  
},  
"execScheduleInfo": {  
"enableReportTimeStamps": 0,  
"numTriggerSources": ,  
"executionMode": 0  
},  
"creationTime": {  
"utcInMilliseconds": 15258  
},  
"failureCounter": 0  
},  
{  
"errorCode": abc,  
"additionalInfo": {  
"length": xyz,  
"result": "XXXXXXXXXXXXXXXXXXX"  
},  
"execScheduleInfo": {  
"enableReportTimeStamps": 0,  
"numTriggerSources": ,  
"executionMode": 0  
},  
"creationTime": {  
"utcInMilliseconds": 15279  
},  
"failureCounter": 0  
},  
]  
}  
I have used below spilt option to get **"result"** as a separate field from above logline.  
split {  
field =\> "[independentFailureReport][failureReportEntries]"  
}  
because of spilt option we are getting 3 separate documents from above logline. But array consist same filed and value so i need to fetch only one value instead of getting 3 separate documents. Could you please guide me on this?

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [March 12, 2024, 6:38pm UTC](https://discuss.elastic.co/t/how-to-fetch-first-value-from-json-array/355214/2 "2024-03-12T18:38:14Z")

</div>

You can use a field reference to extract a value from the first hash in an array. For example

```
input { generator { count => 1 lines => ['{ "a": [ { "x": 1 }, { "y": 2 }] }' ] codec => json } }
output { stdout { codec => rubydebug { metadata => false } } }
filter { mutate { add_field => { "foo" => "%{[a][0][x]}" } } }

```

will produce

```
       "foo" => "1",
         "a" => [
    [0] {
        "x" => 1
    },
    [1] {
        "y" => 2
    }
],

```

---

<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:** [April 9, 2024, 6:38pm UTC](https://discuss.elastic.co/t/how-to-fetch-first-value-from-json-array/355214/3 "2024-04-09T18:38:43Z")

</div>

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