json jq:error:无法用string索引string

nwsw7zdq  于 2023-03-31  发布在  其他
关注(0)|答案(1)|浏览(157)

我正试图从json中提取“ip_address”信息,但在几次失败的尝试之后,我仍然不能理解我做错了什么。
源文件:

[
  {
    "Status": "ACTIVE",
    "External gateway info": "{\"network_id\": \"68719cf1-xxxx\", \"enable_snat\": true, \"external_fixed_ips\": [{\"subnet_id\": \"b55dc621-xxxx\", \"ip_address\": \"10.10.10.11\"}]}",
    "Name": "xxxx",
    "Tags": "",
    "Availability zones": "xxxx",
    "Project": "xxxx",
    "State": "UP",
    "Routes": "",
    "HA": true,
    "ID": "f9f7e5b1-xxxx"
  },
  {
    "Status": "ACTIVE",
    "External gateway info": "{\"network_id\": \"68719cf1-xxxx\", \"enable_snat\": true, \"external_fixed_ips\": [{\"subnet_id\": \"b55dc621-xxxx\", \"ip_address\": \"10.10.10.12\"}]}",
    "Name": "xxxx",
    "Tags": "",
    "Availability zones": "xxxx",
    "Project": "xxxx",
    "State": "UP",
    "Routes": "",
    "HA": true,
    "ID": "fbaa19aa-xxxx"
  },
  {
    "Status": "ACTIVE",
    "External gateway info": "{\"network_id\": \"68719cf1-xxxx\", \"enable_snat\": true, \"external_fixed_ips\": [{\"subnet_id\": \"b55dc621-xxxx\", \"ip_address\": \"10.10.10.13\"}]}",
    "Name": "xxxx",
    "Tags": "",
    "Availability zones": "xxxx",
    "Project": "xxxx",
    "State": "UP",
    "Routes": "",
    "HA": true,
    "ID": "fe2abdd8-xxxx"
  },
  {
    "Status": "ACTIVE",
    "External gateway info": null,
    "Name": "xxxx",
    "Tags": "",
    "Availability zones": "xxxx",
    "Project": "xxxx",
    "State": "UP",
    "Routes": "",
    "HA": true,
    "ID": "ff29444d-xxxx"
  }
]

我尝试使用以下命令/语法:

jq -r '.[]."External gateway info" | {external_fixed_ips} | .[].ip_address'

但得到以下错误:

jq: error (at <stdin>:1934): Cannot index string with string "external_fixed_ips"

我希望从这个json中提取“ip_address”,但失败了。

gab6jxml

gab6jxml1#

."External gateway info"下的值是JSON编码的。使用fromjson解码。组合使用错误抑制运算符?处理键丢失的情况。

jq -r '.[]."External gateway info" | fromjson? | .external_fixed_ips[].ip_address'
10.10.10.11
10.10.10.12
10.10.10.13

相关问题