how to pass multiple args into jq?

2019-09-24 04:24发布

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.

标签: json shell jq
1条回答
该账号已被封号
2楼-- · 2019-09-24 04:49

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
查看更多
登录 后发表回答