I have an input like below which just has stageIds, along with their submit and completion time in unix time seconds
[
{
"stageId": 1,
"submitTime_epoch_secs": 5,
"completionTime_epoch_secs": 10
},
{
"stageId": 2,
"submitTime_epoch_secs": 15,
"completionTime_epoch_secs": 17
},
{
"stageId": 3,
"submitTime_epoch_secs": 29,
"completionTime_epoch_secs": 30
}
]
desired output is below, where each stageId, submit, and completion times are compared with previous and next and the delay is added as another key/val per element.
[
{
"stageId": 1,
"submitTime_epoch_secs": 5,
"completionTime_epoch_secs": 10,
"delayTillNextStageSubmit",5
"delayFromPrevStageComplete",null
},
{
"stageId": 2,
"submitTime_epoch_secs": 15,
"completionTime_epoch_secs": 17,
"delayTillNextStageSubmit",12
"delayFromPrevStageComplete",5
},
{
"stageId": 3,
"submitTime_epoch_secs": 29,
"completionTime_epoch_secs": 30,
"delayTillNextStageSubmit",null
"delayFromPrevStageComplete",12
}
]
here the stageId 1 delayTillNextStageSubmit is difference between stageId 2 submitTime and stageId 1 completion time, 15 – 10 = 5.
is this possible with jq?
I am new to jq, so don’t know how to solve this
2
Answers
Here is a
derivative
method for this kind of task:If
$data
holds your data, then based on my understanding of the question, you would invoke it like this:Please note there seems to be an inconsistency between the input and output for stageId 3 in your example: "submitTime_epoch_secs" is shown as 22 and then as 29.
to_entries
is used to get the index of each entry as.key
.(.key + 1) % length
is used to avoid an error with the last element.