Daniel Daniel - 1 year ago 99
Bash Question

Shell Script more complex jq select

I just like to find a way to search with jq with a shellscript, let explain it better:

I have this json file:

{
"meta": {
"limit": 200,
"next": null,
"offset": 0,
"previous": null,
"total_count": 2
},
"objects": [
{
"bandwidth": 768,
"call_direction": "in",
"call_uuid": "84e6098a-d0a9-44ed-846e-074b6d563cfb",
"conference": "JOG1_VMR6",
"connect_time": "2017-01-26T19:20:01.096940",
"destination_alias": "[email protected]",
"display_name": "JG - Sala 2",
"encryption": "On",
"has_media": true,
"id": "92dab287-0091-4d57-bdff-f37cce6c586e",
"is_muted": false,
"is_on_hold": false,
"is_presentation_supported": true,
"is_presenting": false,
"is_streaming": false,
"license_count": 1,
"license_type": "port",
"media_node": "192.168.20.11",
"parent_id": "",
"participant_alias": "h323:192.168.51.153",
"protocol": "H323",
"remote_address": "192.168.51.153",
"remote_port": 11000,
"resource_uri": "/api/admin/status/v1/participant/92dab287-0091-4d57-bdff-f37cce6c586e/",
"role": "chair",
"service_tag": "JOG1",
"service_type": "conference",
"signalling_node": "192.168.20.11",
"source_alias": "h323:192.168.51.153",
"system_location": "CUSTOMER-JG-LAN",
"vendor": "TANDBERG (Tandberg 529)"
},
{
"bandwidth": 1280,
"call_direction": "in",
"call_uuid": "dd60c9a2-22e0-4685-9a3d-8573e5e6cc75",
"conference": "Sala_Teste-Turn-up",
"connect_time": "2017-01-27T01:42:11.103894",
"destination_alias": "5001",
"display_name": "John",
"encryption": "On",
"has_media": true,
"id": "dd60c9a2-22e0-4685-9a3d-8573e5e6cc75",
"is_muted": false,
"is_on_hold": false,
"is_presentation_supported": false,
"is_presenting": false,
"is_streaming": false,
"license_count": 1,
"license_type": "port",
"media_node": "172.24.25.106",
"parent_id": "",
"participant_alias": "John",
"protocol": "WebRTC",
"remote_address": "179.65.15.9",
"remote_port": 62794,
"resource_uri": "/api/admin/status/v1/participant/dd60c9a2-22e0-4685-9a3d-8573e5e6cc75/",
"role": "chair",
"service_tag": "JOG",
"service_type": "conference",
"signalling_node": "172.24.25.106",
"source_alias": "John",
"vendor": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"
}
]
}


I wish to find an element inside the "conference" Json TAG, when I find the Conference name I wonder how to bring "connect_time" once I had found the conference value:

Here an exemple what I mean:
conference": "JOG1_VMR6"

I wanna find what is the "connect_time" in this particular to parser it so I built this part of code bellow:

time=$(cat service.html 2>/dev/null | jq '.objects[] | select(.conference=='JOG1_VMR6')' | jq ".connect_time" | grep -o "[^\"]*" | grep -o "[^T][0-9].*" | grep -o "[0-9]\{2\}:[0-9]\{2\}:[0-9].")


But the select(.conference=='JOG1_VMR6')' is showing compile error in the shell and won't bring me the "connect_time": "2017-01-26T19:20:01.096940" so I can't parser it correct.

Answer Source

The key to success here is simplicity. Just one invocation of jq:

jq  '.objects[] | select(.conference=="JOG1_VMR6") | .connect_time'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download