如何将 JQ 中数组中的各个元素传递给 xargs

发布于 2025-01-11 05:47:26 字数 1499 浏览 0 评论 0原文

我有一个这样的数组:

[
  {
    "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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此刻的回忆 2025-01-18 05:47:26
jq -j 'map(@json) | join("\u0000")' http/customer/us/customers.json |
   xargs -0 -I % curl -X POST http://localhost:3000/customers -d %

由于 @json 永远不会产生任何换行,因此以下内容也可以工作:

jq -r '.[] | @json' http/customer/us/customers.json |
   xargs -I % curl -X POST http://localhost:3000/customers -d %

Demo on jq play

  • --join-output/-j 按原样打印字符串,而不是打印生成以下内容的 JSON 字符串文字细绳。不添加任何换行符,甚至不添加尾随换行符。

  • --raw-output/-r 按原样打印字符串,而不是打印生成该字符串的 JSON 字符串文字。在每个字符串后添加换行符。

  • "\\0" 不会产生 NUL。您需要 "\u0000" 来实现这一点。

  • <代码>。 | 没用。

  • '%'% 之间没有区别。它们都将 % 传递给 xargs

  • -n 1 没有用。 -I 隐含 -L 1,这使得 -n 1 变得多余。

  • {}% 更常用作替换表达式。但是 % 也可以,所以我没有改变它。

jq -j 'map(@json) | join("\u0000")' http/customer/us/customers.json |
   xargs -0 -I % curl -X POST http://localhost:3000/customers -d %

Since @json never produces any line feeds, the following would also work:

jq -r '.[] | @json' http/customer/us/customers.json |
   xargs -I % curl -X POST http://localhost:3000/customers -d %

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 % to xargs.

  • -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.

伪心 2025-01-18 05:47:26

使用 --join-output (或 -j)选项输出不带换行符的原始文本,并使用 \u0000 创建 <代码>NUL字符:

jq -j 'map(@json) | join("\u0000")' http/customer/us/customers.json \
  | xargs -0 -n 1 -I % curl -X POST http://localhost:3000/customers -d '%'

Use the --join-output (or -j) option to output raw text with no newlines, and use \u0000 to create the NUL character:

jq -j 'map(@json) | join("\u0000")' http/customer/us/customers.json \
  | xargs -0 -n 1 -I % curl -X POST http://localhost:3000/customers -d '%'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文