Skip to main content
added 184 characters in body
Source Link
Kusalananda
  • 345.5k
  • 39
  • 709
  • 1k

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

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, 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" }'

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
added 178 characters in body
Source Link
Kusalananda
  • 345.5k
  • 39
  • 709
  • 1k

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, 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" }'

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

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, 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" }'
Source Link
Kusalananda
  • 345.5k
  • 39
  • 709
  • 1k

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