My Json file has the data like below:
[
{
"id": 47,
"iid": 12,
"project_id": 1,
"status": "pending",
"source": "push",
"ref": "new-pipeline",
"sha": "ab23456789d",
"web_url": "https://example.com/project/pipelines/47",
"created_at": "2022-02-24T11:28:34.085Z",
"updated_at": "2016-08-24T15:32:35.169Z"
},
{
"id": 48,
"iid": 13,
"project_id": 1,
"status": "pending",
"source": "web",
"ref": "new-pipeline",
"sha": "ab23456789d",
"web_url": "https://example.com/project/pipelines/48",
"created_at": "2022-02-23T11:28:34.085Z",
"updated_at": "2016-08-23T15:32:35.169Z"
}
]
I am trying to fetch the IDs which are created in last 15 minutes. But I couldnt get it.
I have tried the below way,
TIM=`date -u "%Y-%m-%dT%H:%M:%S.000Z" -d '-15 minutes'`
jq -r --arg TIMEE "$TIM" '.[]|select((.ref|contains("dev")) and (.updated_at >= "$TIMEE"))|.id' MyJsonFile.json
But this is not working as expected. I dont see any IDs. But when made the condition to (.updated_at >= "$TIMEE")
. I can see all IDs which are created even in last one minute.
Not sure if I am trying in the right way. Any help is much appreciated.
CodePudding user response:
You can check the time within jq, but it handles ISO 8601 dates only without milliseconds. Therefore you have to cut them off for comparison. now
gives you the current time.
jq '.[] | select(.updated_at | "\(.[:-5])Z" | fromdate 900 > now).id'
If you want to have a parameter for minutes, try:
jq --argjson min 15 '
.[] | select(.updated_at | "\(.[:-5])Z" | fromdate 60 * $min > now).id
'
CodePudding user response:
TIM=`date -u "%Y-%m-%dT%H:%M:%S.000Z" -d '-15 minutes'`
jq -r --arg TIMEE "$TIM" '.[]|select((.ref|contains("dev")) and (.updated_at >= $TIMEE))|.id' MyJsonFile.json
just removing the quote around "$TIMEE" will work. It might be taken as a string if you quote it.
can check here: jq playground link with an example