Need to create array from deep level JSON structure

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.

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

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

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.

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.

@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.

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

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