Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make multiple assignment of field values in jq?

Tags:

json

jq

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?

like image 219
David Avatar asked Oct 12 '15 07:10

David


1 Answers

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'
like image 77
Hans Z. Avatar answered Oct 05 '22 11:10

Hans Z.