我正试图从json文件中获取一个数组。
{
"Requests": [
{
"Item1": "2020-01-27 16:24:49",
"Item2": "203i1Kj2gTEQgfdsfds23",
"Item3": 1603,
"Item4": "generic"
},
{
"Item1": "2020-01-27 16:24:49",
"Item2": "203i1Kj2gTEQgfdsfds23",
"Item3": 1603,
"Item4": "generic"
},
{
"Item1": "2020-01-27 16:24:49",
"Item2": "203i1Kj2gTEQgfdsfds23",
"Item3": 1603,
"Item4": "generic"
},
{
"Item1": "2020-01-27 16:24:49",
"Item2": "203i1Kj2gTEQgfdsfds23",
"Item3": 1603,
"Item4": "generic"
}
]
}
然后,我想将这些项目中的每一个传递给curl请求,并可能使用xargs 在parralel中处理该请求
我还在跌跌撞撞地把一个项目拿到卷曲的终点。
cat CurlArgsFile.json | jq -c '.[][0]' | xargs -I % curl -d % -H "Content-Type: application/json" -X POST http://localhost:53391/mySVCS/writeData
我在Windows 10上的Git Bash上运行这个。当我试图将json输出回显到一个文件中时,我得到了下面没有引号的json,现在我不确定这是否是我发送的正确json。
{Item1:2020-01-27 16:24:49,Item2:203i1Kj2gTEQgfdsfds23,Item3:1603,Item4:generic}
如何将json中的第一个项目发送到端点,然后enpoint识别它?
这是最终结果。问题是我不熟悉jq实用程序,我的json不断出错。
cat CurlArgsFile.json | jq -r '.Requests[]|tojson' | xargs -0 -I % curl -d % -H "Content-Type: application/json" -X POST http://localhost:53391/mySVC/writeData
如果使用GNUxargs
,则-d
标志是有用的:
jq '.Requests[]' -rc file.json |
xargs -d 'n' -0 -P0 -I@ curl -X POST -H 'Content-Type: application/json' -d @ http://localhost:53391/mySVC/writeData
否则,请使用tr
使其与其他xargs
(如BSD或BusyBox(兼容。
jq '.Requests[]' -rc file.json |
tr 'n' ' ' |
xargs -0 -P0 -I@ curl -X POST -H 'Content-Type: application/json' -d @ http://localhost:53391/mySVC/writeData