Reputation: 97
I am receiving this json file that lists one or more records. When there are multiple records, records are placed in an array. Otherwise, there is no array. I am trying to extract the sessionkey from the records using jq. Is there a generic way to match both cases:
Sample #1: message with multiple records:
{
"message": {
"header": {
"response": {
"result": "SUCCESS",
"gsbStatus": "PRIMARY"
}
},
"body": {
"bodyContent": {
"type": "history:lstmeetingusageHistoryResponse",
"meetingUsageHistory": [
{
"sessionKey": "1263859083",
"meetingUUID": "603f5595dc8e4c7a9c2esdfsdf494cfae91815",
"confName": "sdfsdf setup for Dsdfdf alerts",
"meetingStartTime": "07/09/2020 14:00:17",
"meetingEndTime": "07/09/2020 15:01:53",
"duration": "62",
"timezone": "GMT-05:00, Central (Chicago)",
"timeZoneID": "7",
"timeZoneWithDST": "Chicago (Central Daylight Time, GMT-05:00)",
"trackingCode": null,
"meetingType": "PRO",
"hostWebExID": "[email protected]",
"hostName": "test Tech",
"hostEmail": "[email protected]",
"totalCallInMinutes": "0",
"totalPeopleMinutes": "192",
"totalCallInTollfreeMinutes": "0",
"totalCallOutDomestic": "0",
"totalCallOutInternational": "0",
"totalCallOutInternal": "0",
"totalCallInInternational": "0",
"tollCallInInternational": "0",
"tollfreeCallInInternational": "0",
"totalVoipMinutes": "192",
"userID": "544798712",
"totalParticipants": "4",
"totalParticipantsVoip": "4",
"totalParticipantsCallIn": "0",
"totalParticipantsCallOut": "0",
"confID": "166058947180001507",
"peakAttendee": "4",
"assistService": null
},
{
"sessionKey": "623922696",
"meetingUUID": "503dsfsdfsdfd4280e4a452e03b9c75dce0f26",
"confName": "test Tech's Personal Room",
"meetingStartTime": "06/24/2020 19:21:57",
"meetingEndTime": "06/24/2020 20:11:42",
"duration": "50",
"timezone": "GMT-05:00, Central (Chicago)",
"timeZoneID": "7",
"timeZoneWithDST": "Chicago (Central Daylight Time, GMT-05:00)",
"trackingCode": null,
"meetingType": "PRO",
"hostWebExID": "[email protected]",
"hostName": "test Tech",
"hostEmail": "[email protected]",
"totalCallInMinutes": "0",
"totalPeopleMinutes": "100",
"totalCallInTollfreeMinutes": "0",
"totalCallOutDomestic": "0",
"totalCallOutInternational": "0",
"totalCallOutInternal": "0",
"totalCallInInternational": "0",
"tollCallInInternational": "0",
"tollfreeCallInInternational": "0",
"totalVoipMinutes": "100",
"userID": "544798712",
"totalParticipants": "2",
"totalParticipantsVoip": "2",
"totalParticipantsCallIn": "0",
"totalParticipantsCallOut": "0",
"confID": "164945117836245416",
"peakAttendee": "2",
"assistService": null
}
],
"matchingRecords": {
"total": "2",
"returned": "2",
"startFrom": "1"
}
}
}
}
}
Sample #2: message with a single record:
{
"message": {
"header": {
"response": {
"result": "SUCCESS",
"gsbStatus": "PRIMARY"
}
},
"body": {
"bodyContent": {
"type": "history:lstmeetingusageHistoryResponse",
"meetingUsageHistory": {
"sessionKey": "297115075",
"meetingUUID": "sdsadfdsfsfsfdsfsdf",
"confName": "abc's Personal Room",
"meetingStartTime": "07/09/2020 20:58:37",
"meetingEndTime": "07/09/2020 22:38:15",
"duration": "100",
"timezone": "GMT-05:00, Central (Chicago)",
"timeZoneID": "7",
"timeZoneWithDST": "Chicago (Central Daylight Time, GMT-05:00)",
"trackingCode": null,
"meetingType": "PRO",
"hostWebExID": "[email protected]",
"hostName": "asdasdasdasd",
"hostEmail": "[email protected]",
"totalCallInMinutes": "0",
"totalPeopleMinutes": "197",
"totalCallInTollfreeMinutes": "0",
"totalCallOutDomestic": "0",
"totalCallOutInternational": "0",
"totalCallOutInternal": "0",
"totalCallInInternational": "0",
"tollCallInInternational": "0",
"tollfreeCallInInternational": "0",
"totalVoipMinutes": "197",
"userID": "487886288",
"totalParticipants": "3",
"totalParticipantsVoip": "3",
"totalParticipantsCallIn": "0",
"totalParticipantsCallOut": "0",
"confID": "166305919935739955",
"peakAttendee": "2",
"assistService": null
},
"matchingRecords": {
"total": "1",
"returned": "1",
"startFrom": "1"
}
}
}
}
}
Thanks
Upvotes: 1
Views: 739
Reputation: 117017
If it is acceptable to print all "sessionKey" values, then the simplest would probably be:
.. | objects | select(has("sessionKey")) | .sessionKey
Otherwise you could use an if ... then ... else ... end
approach.
Upvotes: 1