# Need to create array from deep level JSON structure

**URL:** <https://discuss.elastic.co/t/need-to-create-array-from-deep-level-json-structure/117720>\
**Category:** Logstash\
**Created:** [January 30, 2018, 11:55pm UTC](https://discuss.elastic.co/t/need-to-create-array-from-deep-level-json-structure/117720 "2018-01-30T23:55:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mnaveen\_m](https://avatars.discourse-cdn.com/v4/letter/m/4da419/32.png) [@mnaveen\_m](https://discuss.elastic.co/u/mnaveen_m)\
**Post date:** [January 30, 2018, 11:55pm UTC](https://discuss.elastic.co/t/need-to-create-array-from-deep-level-json-structure/117720/1 "2018-01-30T23:55:03Z")

</div>

Here is our JSON structure:  
"data": {  
"OTAHotelAvailRS": {  
"errorsOrHotelStaysOrRoomStays": [  
{},  
{  
"roomStay": [  
{  
"responseType": "PropertyList",  
"basicPropertyInfo": {  
"hotelCode": "H23428",  
}  
},{  
"responseType": "PropertyList",  
"basicPropertyInfo": {  
"hotelCode": "H877665",  
}  
}, {  
"responseType": "PropertyList",  
"basicPropertyInfo": {  
"hotelCode": "H87678",  
}]  
}  
}]  
}  
}

Would like to collect hotelCodes from JSON and create as a list to show in Kibana. Started writing to create array using ruby code in log stash config as below, but its not giving any NEW array as output in kibana, no errors while starting logstash pipeline.

ruby {  
code =\> 'if(!event.get("[data][OTAHotelAvailRS][errorsOrHotelStaysOrRoomStays][roomStay]").nil?)  
event.set("[hotelCodes]", event.get("[data][OTAHotelAvailRS][errorsOrHotelStaysOrRoomStays][roomStay]").collect { |h| h["basicPropertyInfo"]["hotelCode"] }) end'  
}

Please check if this is correct way to create array from above JSON structure, and help me to solve this or if there is any other way to do this.

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [January 31, 2018, 6:59am UTC](https://discuss.elastic.co/t/need-to-create-array-from-deep-level-json-structure/117720/2 "2018-01-31T06:59:56Z")

</div>

You're referencing `[data][OTAHotelAvailRS]` but there's no mention of `OTAHotelAvailRS` in the example data you've provided.

---

<div class="post-metadata">

**Author:** ![mnaveen\_m](https://avatars.discourse-cdn.com/v4/letter/m/4da419/32.png) [@mnaveen\_m](https://discuss.elastic.co/u/mnaveen_m)\
**Post date:** [January 31, 2018, 2:19pm UTC](https://discuss.elastic.co/t/need-to-create-array-from-deep-level-json-structure/117720/3 "2018-01-31T14:19:16Z")

</div>

Thanks for replying @magnusbaeck. Its my bad- in example JSON, Response is OTAHotelAvailRS. Please help me with this.

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [January 31, 2018, 2:57pm UTC](https://discuss.elastic.co/t/need-to-create-array-from-deep-level-json-structure/117720/4 "2018-01-31T14:57:13Z")

</div>

The problem is that `[data][OTAHotelAvailRS][errorsOrHotelStaysOrRoomStays]` is an array. You'll have to iterate over the items in that array and act on the object with a `roomStay` key. It's the array the `roomStay` key points to that you can run collect on.

---

<div class="post-metadata">

**Author:** ![mnaveen\_m](https://avatars.discourse-cdn.com/v4/letter/m/4da419/32.png) [@mnaveen\_m](https://discuss.elastic.co/u/mnaveen_m)\
**Post date:** [January 31, 2018, 3:19pm UTC](https://discuss.elastic.co/t/need-to-create-array-from-deep-level-json-structure/117720/5 "2018-01-31T15:19:57Z")

</div>

Thanks @magnusbaeck. So the same case with roomStay also? because roomStay is also array of basicPropertyInfo where hotelCode resides.

By the way, can you please help me with sample code how to iterate through array find key in this situation to collect hotelCodes.

---

<div class="post-metadata">

**Author:** ![mnaveen\_m](https://avatars.discourse-cdn.com/v4/letter/m/4da419/32.png) [@mnaveen\_m](https://discuss.elastic.co/u/mnaveen_m)\
**Post date:** [January 31, 2018, 5:22pm UTC](https://discuss.elastic.co/t/need-to-create-array-from-deep-level-json-structure/117720/6 "2018-01-31T17:22:51Z")

</div>

@magnusbaeck, tried to iterate JSON get roomStay array with below ruby code but it returns roomStays as nil value.

ruby {  
code =\> 'value = event.get("[data][OTAHotelAvailRS][errorsOrHotelStaysOrRoomStays]").find { |h| h["roomStay"] }["value"]; event.set("roomStays", value)'  
}

can you please help me with this.

---

<div class="post-metadata">

**Author:** ![mnaveen\_m](https://avatars.discourse-cdn.com/v4/letter/m/4da419/32.png) [@mnaveen\_m](https://discuss.elastic.co/u/mnaveen_m)\
**Post date:** [February 8, 2018, 6:02pm UTC](https://discuss.elastic.co/t/need-to-create-array-from-deep-level-json-structure/117720/7 "2018-02-08T18:02:03Z")

</div>

Have solved this problem. I wanted to add here this ruby code that might be useful for others doing this:

if(!event.get("[data][OTAHotelAvailRS][errorsOrHotelStaysOrRoomStays]").nil?)  
# Find roomStay keyed element from OTAHotelAvailRS to make RoomStays Object  
event.set("[roomStays]", event.get("[data][OTAHotelAvailRS][errorsOrHotelStaysOrRoomStays]").find { |h| h['roomStay'] })  
# Collect all hotelCodes from RoomStays array  
if(!event.get("[roomStays][roomStay]").nil?)  
event.set("[hotelCodes]", event.get("[roomStays][roomStay]").collect { |h| h['basicPropertyInfo']['hotelCode'] })  
end  
end

---

<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:** [March 8, 2018, 6:02pm UTC](https://discuss.elastic.co/t/need-to-create-array-from-deep-level-json-structure/117720/8 "2018-03-08T18:02:04Z")

</div>

Closed on March 8, 2018, 6:02pm UTC

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