It feels natural to do the selection of the element to update using select()
, and then use map()
to map the selection and assignment to all elements of the b
sub-array:
jq '.a.b |= map(select(.name == "name1").param += { new: "QQQ" })' file
or, shorter
jq '.a.b |= map(select(.name == "name1").param.new = "QQQ")' file
Or, you could pick out all the param
entries from the elements of the b
array and update them. This would be closer to your own command that updates the first element of b
unconditionally:
jq '(.a.b[] | select(.name == "name1").param) += { new: "QQQ" }'
or, shorter,
jq '(.a.b[] | select(.name == "name1").param).new = "QQQ"' file