我有一个JSON,希望使用https://github.com/thedevsaddam/gojsonq解析嵌套数组
{
"total": 2,
"page": 1,
"pageSize": 50,
"data": [
{
"id": "vm-12345",
"host_id":"host-12345",
"name": "MyVirtualMachine",
"guestOS": "Windows Server 2019",
"powerState": "poweredOn",
"cpu": 2,
"memoryMB": 4096,
"disks": [
{
"id": "disk-56789",
"name": "Hard disk 1",
"capacityGB": 100,
"type": "thin"
}
],
"networks": [
{
"id": "network-98765",
"name": "VM Network",
"macAddress": "00:50:56:9A:76:5C",
"ipAddress": "192.168.1.100"
}
],
"toolsRunningStatus": "guestToolsRunning",
"connectionState": "connected"
},
{
"id": "vm-67890",
"host_id":"host-67890",
"name": "TestVM",
"guestOS": "Ubuntu 20.04",
"powerState": "poweredOff",
"cpu": 1,
"memoryMB": 2048,
"disks": [
{
"id": "disk-12345",
"name": "Hard disk 1",
"capacityGB": 50,
"type": "thick"
}
],
"networks": [
{
"id": "network-54321",
"name": "VM Network",
"macAddress": "00:50:56:9A:12:34",
"ipAddress": "192.168.1.101"
}
],
"toolsRunningStatus": "guestToolsNotRunning",
"connectionState": "disconnected"
}
]
}
字符串
正如我们在这里看到的,我们在data
下有disks
,我使用的是这个jq_vm.From("data").Select("id", "name", "host_id", "disks.capacityGB").Get()
个
过滤掉,但这不是返回磁盘,可以在这里看到[map[host_id:host-12345 id:vm-12345 name:MyVirtualMachine] map[host_id:host-67890 id:vm-67890 name:TestVM]]
个
有谁能帮我修一下吗?
1条答案
按热度按时间q9rjltbz1#
我认为这里的问题是
disks
是一个列表,所以你不能使用disks.capacityGB
这样的语法来寻址字段。您可以使用辅助Select
过滤它,但这不会给予您从父级访问键的权限。您可以在选择中包含整个
disks
值:字符串
这让你:
型
它看起来像是有一个
jq
的Go端口,它可以让你得到你想要的东西,如下所示:型
它产生:
型