how to pass multiple args into jq?

2019-09-24 04:17发布

问题:

I am trying to write a new json file. I want to define multiple variables then set them with piping to different properties in the new json file.

jq --arg dnb "$DOMAIN_NAME_BUILT" --arg origin "$DOMAIN_ID_BUILT" \ 
   '.Origins.Items[0].DomainName = $dnb' | '.Origins.Items[0].Id = $origin' distconfig.json > "$tmp" && mv "$tmp" distconfig.json

This works with just one variable: --arg NAME VALUE pattern, but when I add in a second arg and use piping jq ... 'x1 = y1 | x2 = y2, e.g. it breaks.

回答1:

pipe should be inside the filter. and, consider using assignment operator to shorten your code:

jq --arg dnb "$DOMAIN_NAME_BUILT" \
   --arg origin "$DOMAIN_ID_BUILT" \
   '.Origins.Items[0] |= ( .DomainName = $dnb | .Id = $origin )' \
distconfig.json > "$tmp" && mv "$tmp" distconfig.json


标签: json shell jq