I have two different json API responses, which contain the information required for further processing, my requirement is to read these json responses, pull the required information, and create a new single file with the below provided format.
first output:
DETAILS=$(echo $RESPONSE | jq ".results[] | select( .name | index(\"$CLUSTER\"))" | jq -r '[.name, .regionName, .diskSizeGB, .instanceSizeName]')
echo $DETAILS
[
"cluster_name",
"region",
100,
"R50"
]
second output:
LATEST_SNAP_ID=$(echo $SNAP_RESPONSE | jq -c ".results[] | select( .createdAt | contains(\"$time_stamp\"))" | jq -r '[.id, .createdAt]')
echo $LATEST_SNAP_ID
[
"1234567890987654321",
"2022-12-20T23:01:56Z"
]
I tried various options with jq, but no luck.
expected output would be:
Note: the response contains the values for all the clusters, and we need to pull only the required values for them.
echo $SNAP_RESPONSE | jq '.results[0, 1]'
{
"cloudProvider": "AWS",
"copyRegions": [],
"createdAt": "2022-12-20T23:01:56Z",
"expiresAt": "2022-12-27T23:03:50Z",
"frequencyType": "daily",
"id": "1234567890987654321",
"links": [
{
"href": "url1",
"rel": "self"
},
{
"href": "url2",
"rel": "url3"
}
],
"mongodVersion": "1.1.1",
"policyItems": [
"12345465672342"
],
"replicaSetName": "cluster_name_1",
"snapshotType": "scheduled",
"status": "completed",
"storageSizeBytes": 23141234,
"type": "replicaSet"
}
{
"cloudProvider": "AWS",
"copyRegions": [],
"createdAt": "2022-12-19T23:03:08Z",
"expiresAt": "2022-12-26T23:05:02Z",
"frequencyType": "daily",
"id": "1234567890987654322",
"links": [
{
"href": "url1",
"rel": "self"
},
{
"href": "url2",
"rel": "url3"
}
],
"mongodVersion": "1.1.1",
"policyItems": [
"12345465672342"
],
"replicaSetName": "cluster_name_2",
"snapshotType": "scheduled",
"status": "completed",
"storageSizeBytes": 32547137,
"type": "replicaSet"
}
Since the $RESPONSE is too big to paste here, selected the required keys.
echo $RESPONSE | jq '.results[0, 1]' | jq '[.name, regionName, .diskSizeGB, instanceSizeName]'
[
"cluster_name_1",
"region",
10,
"M20"
]
[
"Cluster_name_2",
"region",
160,
"R50"
]
Please assist.