如何将多个args传递给jq?

人气:275 发布:2022-10-16 标签: json shell jq

问题描述

我正在尝试编写一个新的json文件.我想定义多个变量,然后使用管道将它们设置为新json文件中的不同属性.

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

这仅适用于一个变量:--arg NAME VALUE模式,但是当我添加第二个arg并使用管道jq ... 'x1 = y1 | x2 = y2, e.g.时,它将中断.

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.

推荐答案

管道应在过滤器内.并且,请考虑使用赋值运算符来缩短您的代码:

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

999