0

Given that i have the below json:

{
  "id": "images",
  "type": "collection",
  "href": "https://api.ionos.com/cloudapi/v5/images",
  "items": [
    {
      "id": "2bdfc61e-1ff7-11ea-af1c-525400f64d8d",
      "type": "image",
      "href": "https://api.ionos.com/cloudapi/v5/images/2bdfc61e-1ff7-11ea-af1c-525400f64d8d",
      "metadata": {
        "etag": "b1a133b9ac038de9157572bbc3491898",
        "createdDate": "2019-12-16T11:28:24Z",
        "createdBy": "System",
        "createdByUserId": "System",
        "lastModifiedDate": "2019-12-16T11:28:24Z",
        "lastModifiedBy": "System",
        "lastModifiedByUserId": "System",
        "state": "AVAILABLE"
      },
      "properties": {
        "name": "windows-2008-r2-server-2019-12",
        "description": "",
        "location": "gb/lhr",
        "size": 18,
        "cpuHotPlug": true,
        "cpuHotUnplug": false,
        "ramHotPlug": false,
        "ramHotUnplug": false,
        "nicHotPlug": true,
        "nicHotUnplug": true,
        "discVirtioHotPlug": true,
        "discVirtioHotUnplug": false,
        "discScsiHotPlug": false,
        "discScsiHotUnplug": false,
        "licenceType": "WINDOWS",
        "imageType": "HDD",
        "imageAliases": [
          "windows:2008r2"
        ],
        "public": true
      }
    },
    {
      "id": "d65c184b-cd78-11e9-b88c-525400f64d8d",
      "type": "image",
      "href": "https://api.ionos.com/cloudapi/v5/images/d65c184b-cd78-11e9-b88c-525400f64d8d",
      "metadata": {
        "etag": "f3f490ba129dbb56b4dd0f97870240b8",
        "createdDate": "2019-09-02T11:57:28Z",
        "createdBy": "System",
        "createdByUserId": "System",
        "lastModifiedDate": "2019-09-02T11:57:28Z",
        "lastModifiedBy": "System",
        "lastModifiedByUserId": "System",
        "state": "AVAILABLE"
      },
      "properties": {
        "name": "ubuntu-16.04.6-server-amd64.iso",
        "description": "",
        "location": "gb/lhr",
        "size": 0.86,
        "cpuHotPlug": true,
        "cpuHotUnplug": false,
        "ramHotPlug": true,
        "ramHotUnplug": false,
        "nicHotPlug": true,
        "nicHotUnplug": true,
        "discVirtioHotPlug": true,
        "discVirtioHotUnplug": true,
        "discScsiHotPlug": false,
        "discScsiHotUnplug": false,
        "licenceType": "LINUX",
        "imageType": "HDD",
        "imageAliases": [
          "ubuntu:16.04_iso"
        ],
        "public": true
      }
    },

  ]
}

I need to find the first id among items which its properties feature, has "licenceType": "LINUX", and "imageType": "HDD".

Here is the code that i tried :

$.items[?(@.properties.imageType=="HDD" 
        @.properties.licenceType=="LINUX" )].id

In my example, it should return d65c184b-cd78-11e9-b88c-525400f64d8d

But, it does not give me anything.

Jeff
  • 7,767
  • 28
  • 85
  • 138

1 Answers1

1

you have to AND both queries so this should work

$..items[?(@.properties.imageType=="HDD" && @.properties.licenceType=="LINUX" )].id

if first element is required then

$..items[0].id
Prany
  • 2,078
  • 2
  • 13
  • 31
  • Thanks. How can i get the first element of the array. i tried `id[0]` but it did not work – Jeff Jan 24 '20 at 10:09
  • @Jeff - should be like this - $..items[0].id – Prany Jan 24 '20 at 10:13
  • added in the answer – Prany Jan 24 '20 at 10:18
  • No, i meant, first element, after you applied this condition `?(@.properties.imageType=="HDD" && @.properties.licenceType=="LINUX" )`. The current answer returns an array of the `IDs`, but i need only the first one – Jeff Jan 24 '20 at 10:19
  • 1
    if you are using any programming language then you could do it by the ("$..items[?(@.properties.imageType=="HDD" && @.properties.licenceType=="LINUX" )].id ").first(); because it is an array of strings – Prany Jan 24 '20 at 10:41