Curl jq-添加使用外部命令计算的条目

Curl jq-添加使用外部命令计算的条目,curl,jq,xargs,Curl,Jq,Xargs,我的意见: { “数据”:[ { “名称”:“名称1”, “Url”:”https://some-content.com" }, { “名称”:“名称2”, “Url”:”https://soming-else.com" } } 我想用curl检查每个Url的内容类型,并将其添加到每个对象中。因此,我将得到: { “数据”:[ { “名称”:“名称1”, “Url”:”https://some-content.com", “内容类型”:“视频” }, { “名称”:“名称2”, “Url”:”

我的意见:

{
“数据”:[
{
“名称”:“名称1”,
“Url”:”https://some-content.com"
},
{
“名称”:“名称2”,
“Url”:”https://soming-else.com"
}
}
我想用
curl
检查每个
Url
内容类型
,并将其添加到每个对象中。因此,我将得到:

{
“数据”:[
{
“名称”:“名称1”,
“Url”:”https://some-content.com",
“内容类型”:“视频”
},
{
“名称”:“名称2”,
“Url”:”https://soming-else.com",
“ContentType”:“文本”
}
}
我用来检查
内容类型
响应标题值的命令:

curl-sSL-o/dev/null-w“{content\u type}”
我觉得
xargs
是正确的选择,但我还没有弄清楚如何以上述方式将结果添加到输入JSON中

我也知道jq的
=|
操作符,但我不知道如何在其中获得
xrags
的结果


我的问题有解决方案吗?可以用我正在尝试使用的工具来解决吗?

关于
|=
与更新输入的相关性,您是对的,我假设它位于一个名为input.json的文件中

不幸的是,正如您已经注意到的,jq不支持“外部”评估,但这里有一种方法至少是直接的,只需要两次调用jq:

while read url ; do
    curl -sSL "$url" -o /dev/null -w '%{content_type}'
done < <(jq -r .data[].Url input.json) | 
jq -Rn --argfile json input.json '
  [inputs] as $in
  | $json
  | .data |= reduce range(0;length) as $i (.;
               .[$i].ContentType = $in[$i])
'
读取url时;执行以下操作
curl-sSL“$url”-o/dev/null-w“{content\u type}”

完成创建内容类型数组并将其合并回原始JSON:

jq '.data[].Url' input.json |\
xargs -n1 curl -sSL -o /dev/null -w '%{content_type}\n' |\
jq --slurp --raw-input 'split("\n")[:-1] | map({"Content-Type": .})' |\
jq -n --argfile in1 input.json --argfile in2 /dev/stdin '$in1 | .data |= ([., $in2] | transpose| map(add))'