Issue
I would like to update multiple dates in a json file. My input json contains many properties but the following is the extracted part that matters: I want to parse the date in metadata, here 2022-07-27, replace it by today's date (e.g. 2022-08-05), and set the delta (here 9 days), and add that delta to all other date found in "data_1h" / "time"
edit: (first forgotten) I need also that metadata's date get eventually replaced by today's date.
I could wrote a small tool in any language, but I would like a linux script that can be run from a gitlab pipeline. It is about preparing mockdata for some tests.
So I started fighting with jq, awk or sed, but am a bit confused there. Maybe an experienced jq guy would find the solution immediately?
{
"metadata":
{
"modelrun_utc": "2022-07-27 00:00",
"modelrun_updatetime_utc": "2022-07-27 07:27"
},
"data_1h":
{
"time": ["2022-07-27 00:00", "2022-07-27 01:00", "2022-08-03 11:00", "2022-08-03 12:00", "2022-08-03 13:00", "2022-08-03 14:00"]
}
}
Any idea?
pseudo code would be:
base_date_str=$(jq .metadata.modelrun_utc $1)
echo $base_date_str
base_date=$(date -d $base_date_str)
today=$(date)
delta=$base_date-$today
input-data=$(jq .data_1h.time $1)
foreach (s in $input-data)
# transform s to date d, add delta to d, replace s by d in output string
replace modelrun_utc modelrun_updatetime_utc by today's date only, keeping the time.
# write output json
How does this look like in real shell commands?
Expected output:
{
"metadata": {
"modelrun_utc": "2022-08-05 00:00",
"modelrun_updatetime_utc": "2022-08-05 07:27"
},
"data_1h": {
"time": [
"2022-08-05 00:00",
"2022-08-05 01:00",
"2022-08-12 11:00",
"2022-08-12 12:00",
"2022-08-12 13:00",
"2022-08-12 14:00"
]
}
}
Solution
Here's one way using jq
logic, not shell commands:
jq '(
.metadata.modelrun_utc | strptime("%Y-%m-%d %H:%M")
| (now - mktime) / (24 * 60 * 60)
) as $diffdays | .data_1h.time[] |= (
strptime("%Y-%m-%d %H:%M") | .[2] += $diffdays
| mktime | strftime("%Y-%m-%d %H:%M")
)'
{
"metadata": {
"modelrun_utc": "2022-07-27 00:00",
"modelrun_updatetime_utc": "2022-07-27 07:27"
},
"data_1h": {
"time": [
"2022-08-05 00:00",
"2022-08-05 01:00",
"2022-08-12 11:00",
"2022-08-12 12:00",
"2022-08-12 13:00",
"2022-08-12 14:00"
]
}
}
Answered By - pmf Answer Checked By - Cary Denson (WPSolving Admin)