【问题标题】:Select multiple JSON values from same key with jq?使用 jq 从同一个键中选择多个 JSON 值?
【发布时间】:2021-10-07 13:19:29
【问题描述】:

我有以下 JSON 输出:

$ system_profiler -json SPPCIDataType | jq -r '.SPPCIDataType[]'
{
  "_name": "Radeon RX 580",
  "sppci_bus": "sppci_pci_device",
  "sppci_device_type": "sppci_displaycontroller",
  "sppci_device-id": "0x67df",
  "sppci_driver_installed": "affirmative_string",
  "sppci_link-speed": "5.0 GT/s",
  "sppci_link-width": "x16",
  "sppci_msi": "Yes",
  "sppci_name": "ATY,AMD,RadeonFramebuffer",
  "sppci_revision-id": "0x00e7",
  "sppci_slot_name": "Slot-1",
  "sppci_subsystem-id": "0xe353",
  "sppci_subsystem-vendor-id": "0x1da2",
  "sppci_vendor-id": "0x1002"
}
{
  "_name": "pci1002,aaf0",
  "sppci_bus": "sppci_pci_device",
  "sppci_device_type": "sppci_audiodevice",
  "sppci_device-id": "0xaaf0",
  "sppci_driver_installed": "affirmative_string",
  "sppci_link-speed": "5.0 GT/s",
  "sppci_link-width": "x16",
  "sppci_msi": "Yes",
  "sppci_revision-id": "0x0000",
  "sppci_slot_name": "Slot-1",
  "sppci_subsystem-id": "0xaaf0",
  "sppci_subsystem-vendor-id": "0x1da2",
  "sppci_vendor-id": "0x1002"
}
{
  "_name": "PXS3",
  "sppci_bus": "sppci_pci_device",
  "sppci_device_type": "sppci_usbxhci",
  "sppci_device-id": "0x1242",
  "sppci_driver_installed": "affirmative_string",
  "sppci_link-speed": "5.0 GT/s",
  "sppci_link-width": "x2",
  "sppci_msi": "Yes",
  "sppci_revision-id": "0x0000",
  "sppci_slot_name": "Slot-3",
  "sppci_subsystem-id": "0x7230",
  "sppci_subsystem-vendor-id": "0x16b8",
  "sppci_vendor-id": "0x1b21"
}
{
  "_name": "pci144d,a808",
  "sppci_bus": "sppci_pci_device",
  "sppci_device_type": "sppci_nvme",
  "sppci_device-id": "0xa808",
  "sppci_driver_installed": "affirmative_string",
  "sppci_link-speed": "8.0 GT/s",
  "sppci_link-width": "x4",
  "sppci_msi": "Yes",
  "sppci_revision-id": "0x0000",
  "sppci_slot_name": "Slot-2@8,0,0",
  "sppci_subsystem-id": "0xa801",
  "sppci_subsystem-vendor-id": "0x144d",
  "sppci_vendor-id": "0x144d"
}
{
  "_name": "pci144d,a808",
  "sppci_bus": "sppci_pci_device",
  "sppci_device_type": "sppci_nvme",
  "sppci_device-id": "0xa808",
  "sppci_driver_installed": "affirmative_string",
  "sppci_link-speed": "8.0 GT/s",
  "sppci_link-width": "x4",
  "sppci_msi": "Yes",
  "sppci_revision-id": "0x0000",
  "sppci_slot_name": "Slot-2@9,0,0",
  "sppci_subsystem-id": "0xa801",
  "sppci_subsystem-vendor-id": "0x144d",
  "sppci_vendor-id": "0x144d"
}

sppci_device_type 等于sppci_nvme 时,我目前正在选择_name

$ system_profiler -json SPPCIDataType | jq -r '.SPPCIDataType[] | select(.sppci_device_type == "sppci_nvme")._name'
pci144d,a808
pci144d,a808

对于匹配多个值中的任何一个的键,正确的 select 语句是什么?比如:

select(.sppci_device_type EQUAL TO ("sppci_audiodevice" OR "sppci_nvme"))._name

【问题讨论】:

    标签: json jq


    【解决方案1】:
    select(.sppci_device_type | . == "sppci_audiodevice" or . == "sppci_nvme") ._name
    

    select(.sppci_device_type | IN("sppci_audiodevice", "sppci_nvme")) ._name
    

    【讨论】:

    • 谢谢,我选择了第二个,因为它更紧凑。
    • @Floren 是的,不客气。我发布了两个,因为第二个在旧版本的 JQ 下不起作用。
    猜你喜欢
    • 2020-03-13
    • 1970-01-01
    • 2022-01-14
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 2019-02-18
    相关资源
    最近更新 更多