将 jq 输出传递给 curl
Pass jq output to curl
我想使用 jq 中的一些值通过管道传输到 curl。我有 json 个结构类似于这样的数据
{
"apps": {
"code": "200",
"name": "app1",
},
"result": "1"
}
{
"apps": {
"code": "200",
"name": "app2",
},
"result": "5"
}
...
...
...
我想做的是获取每个 apps.name 的值和结果并将其传递给 curl:
curl -XPOST http://localhost --data-raw 'app=$appsName result=$result'
curl 命令将 运行 X 次,具体取决于对象的数量。
在 jq 中,我想到了这个,但我不知道如何或最好的方式将它传递给循环卷曲。
jq -r '.result as $result | .apps as $apps | $apps.name + " " + $result myfile.json
jq -r '"\( .apps.name ) \( .result )"' |
while read -r app result; do
curl -XPOST http://localhost --data-raw "app=$app result=$result"
done
或
jq -r '"app=\( .apps.name ) result=\( .result )"' |
while read -r data; do
curl -XPOST http://localhost --data-raw "$data"
done
第一个假定名称不能包含空格。
也许是这样的?
jq < data.json '[.apps.name, .result]|@tsv' -r | while read appsName result; do
curl -XPOST http://localhost --data-raw "$appsName $result"
done
这样可以吗?
while read -r line; do
curl -XPOST http://localhost --data-raw "$line"
done < <(jq -r '"app=\(.apps.name) result=\(.result)"' myfile.json)
我认为 xargs 比 while 循环更好。也许只是个人喜好。
jq -r '"app=\( .apps.name ) result=\( .result )"' \
| xargs -r -I{} curl -XPOST http://localhost --data-raw "{}"
我想使用 jq 中的一些值通过管道传输到 curl。我有 json 个结构类似于这样的数据
{
"apps": {
"code": "200",
"name": "app1",
},
"result": "1"
}
{
"apps": {
"code": "200",
"name": "app2",
},
"result": "5"
}
...
...
...
我想做的是获取每个 apps.name 的值和结果并将其传递给 curl:
curl -XPOST http://localhost --data-raw 'app=$appsName result=$result'
curl 命令将 运行 X 次,具体取决于对象的数量。 在 jq 中,我想到了这个,但我不知道如何或最好的方式将它传递给循环卷曲。
jq -r '.result as $result | .apps as $apps | $apps.name + " " + $result myfile.json
jq -r '"\( .apps.name ) \( .result )"' |
while read -r app result; do
curl -XPOST http://localhost --data-raw "app=$app result=$result"
done
或
jq -r '"app=\( .apps.name ) result=\( .result )"' |
while read -r data; do
curl -XPOST http://localhost --data-raw "$data"
done
第一个假定名称不能包含空格。
也许是这样的?
jq < data.json '[.apps.name, .result]|@tsv' -r | while read appsName result; do
curl -XPOST http://localhost --data-raw "$appsName $result"
done
这样可以吗?
while read -r line; do
curl -XPOST http://localhost --data-raw "$line"
done < <(jq -r '"app=\(.apps.name) result=\(.result)"' myfile.json)
我认为 xargs 比 while 循环更好。也许只是个人喜好。
jq -r '"app=\( .apps.name ) result=\( .result )"' \
| xargs -r -I{} curl -XPOST http://localhost --data-raw "{}"