Home > Software engineering >  Select multiple JSON values from same key with jq?
Select multiple JSON values from same key with jq?

Time:10-08

I have the following JSON output:

$ 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"
}

I'm currently selecting the _name when sppci_device_type is equal to sppci_nvme:

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

What is the correct select statement for a key matching any one of multiple values? Something like:

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

CodePudding user response:

select(.sppci_device_type | . == "sppci_audiodevice" or . == "sppci_nvme") ._name

or

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