如何使用gojsonq解析JSON中的嵌套数组

bksxznpy  于 2023-08-08  发布在  Go
关注(0)|答案(1)|浏览(135)

我有一个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]]
有谁能帮我修一下吗?

q9rjltbz

q9rjltbz1#

我认为这里的问题是disks是一个列表,所以你不能使用disks.capacityGB这样的语法来寻址字段。您可以使用辅助Select过滤它,但这不会给予您从父级访问键的权限。
您可以在选择中包含整个disks值:

func main() {
  jq_vm := gojsonq.New().FromString(data)
  selected := jq_vm.From("data").Select("id", "name", "disks")
  out, _ := json.MarshalIndent(selected.Get(), "", "  ")
  fmt.Print(string(out))
}

字符串
这让你:

[
  {
    "disks": [
      {
        "capacityGB": 100,
        "id": "disk-56789",
        "name": "Hard disk 1",
        "type": "thin"
      }
    ],
    "id": "vm-12345",
    "name": "MyVirtualMachine"
  },
  {
    "disks": [
      {
        "capacityGB": 50,
        "id": "disk-12345",
        "name": "Hard disk 1",
        "type": "thick"
      }
    ],
    "id": "vm-67890",
    "name": "TestVM"
  }
]


它看起来像是有一个jq的Go端口,它可以让你得到你想要的东西,如下所示:

func main() {
  query, _ := gojq.Parse(".data[]|{id: .id, name: .name, disks: [.disks[].capacityGB]}")
  doc := make(map[string]interface{})
  json.Unmarshal(data, &doc)
  iter := query.Run(doc)
  for {
    v, ok := iter.Next()
    if !ok {
      break
    }
    if err, ok := v.(error); ok {
      log.Fatalln(err)
    }
    out, _ := json.MarshalIndent(v, "", "  ")
    fmt.Println(string(out))
  }
}


它产生:

{
  "disks": [
    100
  ],
  "id": "vm-12345",
  "name": "MyVirtualMachine"
}
{
  "disks": [
    50
  ],
  "id": "vm-67890",
  "name": "TestVM"
}

相关问题