Reputation: 3441
I have jq command like this:
jq --arg ts "$TS" '.Date = $ts, .Marker.Date = $ts, .InfoFromTerminator.Timestamp = $ts'
but it appears to only replace the last item keeping the previous two as is. How do I rewrite the query to replace for all 3 parameters?
Upvotes: 23
Views: 15224
Reputation: 43400
You can also use comma to assign a collection of properties to the same value:
jq --arg ts "$TS" '(.StartDate, .EndDate) = $ts'
This takes the list of path references on the left-hand side and updates them all to the new value. This can be useful when the right-hand expression is more than a simple variable.
Example:
$ jq '(.a, .b) = "new value"' <<< '{"a": "a", "b": "b", "c": "c"}'
{
"a": "new value",
"b": "new value",
"c": "c"
}
Upvotes: 1
Reputation: 54078
Comma is an operator in jq
:
Even the comma operator is a generator, generating first the values generated by the expression to the left of the comma, then for each of those, the values generate by the expression on the right of the comma.
Changing multiple elements can be done by piping from one filter/assignment in to the next as follows:
jq --arg ts "$TS" '.Date = $ts | .Marker.Date = $ts | .InfoFromTerminator.Timestamp = $ts'
Upvotes: 39