[英]curl json object as POST data from json array file using jq in a loop
這是我的示例test.json文件
[
{
"description": "Some description",
"name": "Some name",
"summary": "Some summary. "
},
{
"description": "Some description",
"name": "Some name",
"summary": "Some summary. "
},
{
"description": "Some description",
"name": "Some name",
"summary": "Some summary. "
}
]
這是bash腳本
declare -A values=( )
while IFS= read -r value; do
echo "Read $value" >&2
#echo $value | curl -d @- -H "Content-Type: application/json" https://example.com/api/foo
done < <(jq -r '.[]' <$1)
這是我得到的輸出
Read {
Read "description": "Some description",
Read "name": "Some name",
Read "summary": "Some summary. "
Read }
Read {
Read "description": "Some description",
Read "name": "Some name",
Read "summary": "Some summary. "
Read }
Read {
Read "description": "Some description",
Read "name": "Some name",
Read "summary": "Some summary. "
Read }
我想使用文件中的JSON對象,以使用curl將數據發布到API。 我希望循環運行三遍。
如何獲取整個JSON對象(導致循環運行3次),而不是從test.json文件輸出每一行?
使用jq -c
在單獨的行上發出每個結果。 當您的預期輸出為JSON而非原始字符串時, 請勿使用-r
。
我還在下面添加了一些引號,您的代碼中沒有這些引號:
# for readability, factored out
args=( -d @- -H "Content-Type: application/json" )
while IFS= read -r value; do
echo "Read $value" >&2
curl "${args[@]}" https://example.com/api/foo <<<"$value"
done < <(jq -c '.[]' <"$1")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.