我有这个test.json文件
{
"list": [
{
"expand": "xx",
"id": "xxxxx",
"self": "https",
"key": "test-11",
"fields": {
"field_1": "1234"
}
},
{
"expand": "xx",
"id": "xxxxx",
"self": "https",
"key": "test-10",
"fields": {
"field_1": "1235",
"field_2": null
}
}
]
}我试图使用IFS while循环读取这些值:
cat test.json| jq -r '[.list[]| ([.key]|tostring) + "," + "\(.fields| .field_1)"]|@tsv' \
| while IFS="," read -r key field_1; do
echo "$key $field_1"
curl -s https ://thisistest.com/api/$key/$field_1
done;
echo output displays only: test-11我希望获得key和field_1的值,以便在curl请求中使用它们。
发布于 2020-09-14 03:53:38
注释中@Kev的jq命令将提取您想要的值,每一行每对,如下所示:
> jq -r '.list[]|[.key,.fields.field_1]|@tsv' test.json
test-11 1234
test-10 1235下面是如何修改脚本,使每个值对都能获得curl请求。
#!/bin/bash
while read -r key field_1; do
echo curl -s https://thisistest.com/api/$key/$field_1
done < <( jq -r '.list[]|[.key,.fields.field_1]|@tsv' test.json )如果您对测试满意,请删除echo。
https://stackoverflow.com/questions/63877660
复制相似问题