使用JQ中的JSON中的两个字段

发布于 2025-02-13 16:03:20 字数 1182 浏览 1 评论 0原文

我想在第二个IP中进行这样的输出:

intranet.site.com:59.89.8.8

尝试运行的命令

cat ./droplet_list.json | jq '.[] | .name , .networks.v4[].ip_address'

运行我正在

"intranet.site.com"
"10.136.95.40"
"59.89.8.8"
"57.23.3.1"

cat ./droplet_list.json | jq '.[] | .name , .networks.v4[].ip_address[1]'

并收到错误

 “ intranet.site.com”
JQ:错误(AT< stdin>:36035):无法用数字索引字符串
 

我的JSON文件

[
  {
    "id": 106,
    "name": "intranet.site.com",
    "networks": {
      "v4": [
        {
          "ip_address": "10.136.95.40",
          "netmask": "255.255.0.0",
          "gateway": "10.136.0.1"
        },
        {
          "ip_address": "59.89.8.8",
          "netmask": "255.255.240.0",
          "gateway": "159.0.0.1"
        },
        {
          "ip_address": "57.23.3.1",
          "netmask": "255.255.252.0",
          "gateway": "157.0.0.1"
        }
      ]
    }
  }
]

谢谢

I would like to have output like this with second IP:

intranet.site.com : 59.89.8.8

running this command

cat ./droplet_list.json | jq '.[] | .name , .networks.v4[].ip_address'

I have got

"intranet.site.com"
"10.136.95.40"
"59.89.8.8"
"57.23.3.1"

I was trying run:

cat ./droplet_list.json | jq '.[] | .name , .networks.v4[].ip_address[1]'

and got the error

"intranet.site.com"
jq: error (at <stdin>:36035): Cannot index string with number

My JSON file

[
  {
    "id": 106,
    "name": "intranet.site.com",
    "networks": {
      "v4": [
        {
          "ip_address": "10.136.95.40",
          "netmask": "255.255.0.0",
          "gateway": "10.136.0.1"
        },
        {
          "ip_address": "59.89.8.8",
          "netmask": "255.255.240.0",
          "gateway": "159.0.0.1"
        },
        {
          "ip_address": "57.23.3.1",
          "netmask": "255.255.252.0",
          "gateway": "157.0.0.1"
        }
      ]
    }
  }
]

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

不念旧人 2025-02-20 16:03:20

只需使用+与字符串相连:

jq -r '.[] | .name + ": " + .networks.v4[].ip_address'
intranet.site.com: 10.136.95.40
intranet.site.com: 59.89.8.8
intranet.site.com: 57.23.3.1

demo

您也可以给您<<<代码> .v4 数组特定索引(而不是用[]在其所有项目上迭代):

jq - '.[] | .name + ": " + .networks.v4[1].ip_address'
intranet.site.com: 59.89.8.8

demo

Just use + to concatenate the strings:

jq -r '.[] | .name + ": " + .networks.v4[].ip_address'
intranet.site.com: 10.136.95.40
intranet.site.com: 59.89.8.8
intranet.site.com: 57.23.3.1

Demo

You may also give the .v4 array specific indices (instead of iterating over all of its items with []):

jq - '.[] | .name + ": " + .networks.v4[1].ip_address'
intranet.site.com: 59.89.8.8

Demo

长不大的小祸害 2025-02-20 16:03:20

一个选项是将 以及 + 运算符,例如

jq -r 'map(.name + ": " + .networks.v4[].ip_address)[]' yourfile.json

demo

One option would be using map along with + operator such as

jq -r 'map(.name + ": " + .networks.v4[].ip_address)[]' yourfile.json

Demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文