Assuming version
is a top-level key in your JSON document, you would update it to the value that you want using jq
like so:
jq --arg patch 3434 \
'.version |= (now | strftime("%y.%j.") + $patch)' file.json >newfile.json
This first sets the internal variable $patch
to the patch release version on the command line. It then formats a time-stamp string from the current time using strftime()
and adds the value of $patch
on to the end of that string. This resulting string is then assigned to the version
key at the top-level of the JSON document, and the resulting document is outputted.
Example:
$ cat file.json
{
"key": "value",
"version": "0.0.0",
"foo": "bar"
}
$ jq --arg patch 3434 '.version |= (now | strftime("%y.%j.") + $patch)' file.json
{
"key": "value",
"version": "21.292.3434",
"foo": "bar"
}
Would you need to make sure that the old version is not updated if it is anything other than exactly 0.0.0
, then instead use
jq --arg patch 3434 \
'select(.version == "0.0.0").version |= (now | strftime("%y.%j.") + $patch)' file.json >newfile.json
That is, use select()
to ensure that the object is only ever updated if its version
value is 0.0.0
.
An alternative way of formulating this, which may look nicer to some:
jq --arg patch 3434 \
'(now | strftime("%y.%j.") + $patch) as $version |
select(.version == "0.0.0").version |= $version' file.json >newfile.json
old_version
variable contents contain literal backslashes: your json data most likely does not. See 3.1.2.2 Single Quotes in the manual