如何将 JQ 中数组中的各个元素传递给 xargs
我有一个这样的数组:
[
{
"customer": {
"id": "61ae22891dbcb5200f4af298",
"customerId": "C0001346",
"companyName": "Real Constructions",
"displayName": "Real Constructions",
"fullName": "NiyatiU",
"email": "[email protected]",
"phone": "(987) 654-3209"
},
"address": {
"streetName": "NY",
"state": "NY",
"city": "NY",
"zipcode": "39092"
}
},
{
"customer": {
"id": "61ae29f31dbcb5200f4af2b2",
"customerId": "C0001347",
"companyName": "UK Reality",
"displayName": "UK Reality",
"fullName": "VatsalShah",
"email": "[email protected]",
"phone": "(766) 688-1348"
},
"address": {
"streetName": "Queens",
"state": "NY",
"city": "Queens",
"zipcode": "11354"
}
}
]
我想将每个对象作为单独的参数传递给 xargs 和 curl。伪代码如下
jq '. | map(@json) | join("\\0")' http/customer/us/customers.json \
| xargs -0 -n 1 -I % curl -X POST http://localhost:3000/customers -d '%'
但我得到:
xargs: insufficient space for argument
因为我认为 json 元素太大了。我不想写一堆文件。
I have an array like such:
[
{
"customer": {
"id": "61ae22891dbcb5200f4af298",
"customerId": "C0001346",
"companyName": "Real Constructions",
"displayName": "Real Constructions",
"fullName": "NiyatiU",
"email": "[email protected]",
"phone": "(987) 654-3209"
},
"address": {
"streetName": "NY",
"state": "NY",
"city": "NY",
"zipcode": "39092"
}
},
{
"customer": {
"id": "61ae29f31dbcb5200f4af2b2",
"customerId": "C0001347",
"companyName": "UK Reality",
"displayName": "UK Reality",
"fullName": "VatsalShah",
"email": "[email protected]",
"phone": "(766) 688-1348"
},
"address": {
"streetName": "Queens",
"state": "NY",
"city": "Queens",
"zipcode": "11354"
}
}
]
and I would like to pass each object as a seperate param to xargs and curl. Psuedo code like
jq '. | map(@json) | join("\\0")' http/customer/us/customers.json \
| xargs -0 -n 1 -I % curl -X POST http://localhost:3000/customers -d '%'
But then I get:
xargs: insufficient space for argument
because the json element is too big, I'd assume. I'd prefer not write a bunch of files.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于
@json
永远不会产生任何换行,因此以下内容也可以工作:Demo on jq play
--join-output
/-j
按原样打印字符串,而不是打印生成以下内容的 JSON 字符串文字细绳。不添加任何换行符,甚至不添加尾随换行符。--raw-output
/-r
按原样打印字符串,而不是打印生成该字符串的 JSON 字符串文字。在每个字符串后添加换行符。"\\0"
不会产生 NUL。您需要"\u0000"
来实现这一点。<代码>。 | 没用。
'%'
和%
之间没有区别。它们都将%
传递给xargs
。-n 1
没有用。-I
隐含-L 1
,这使得-n 1
变得多余。{}
比%
更常用作替换表达式。但是%
也可以,所以我没有改变它。Since
@json
never produces any line feeds, the following would also work:Demo on jq play
--join-output
/-j
prints the string as-is rather that rather than printing a JSON string literal that produces the string. Doesn't add any line feeds, not even a trailing one.--raw-output
/-r
prints the string as-is rather that rather than printing a JSON string literal that produces the string. Adds a line feed after each string."\\0"
doesn't produce a NUL. You want"\u0000"
for that.. |
is useless.There's no difference between
'%'
and%
. They both pass%
toxargs
.-n 1
is useless.-I
implies-L 1
, which renders-n 1
redundant.{}
is more commonly used as the replacement expression than%
. But%
is also fine, so I didn't change this.使用
--join-output
(或-j
)选项输出不带换行符的原始文本,并使用\u0000
创建 <代码>NUL字符:Use the
--join-output
(or-j
) option to output raw text with no newlines, and use\u0000
to create theNUL
character: