MyName
MyName

Reputation: 370

Get N number of values from object with jq

I'm trying to retrieve a given number of values from the following json object (just used 2 for brevity):

[
     {
    "content": null,
    "deleted": false,
    "metadata": null,
    "name": "name/windows-2016-osDisk.170b8936-d66d-4139-a409-26f4e9d354fe.vhd",
    "properties": {
      "appendBlobCommittedBlockCount": null,
      "blobTier": "P10",
      "blobTierChangeTime": null,
      "blobTierInferred": true,
      "blobType": "PageBlob",
      "contentLength": 136367309312,
      "contentRange": null,
      "contentSettings": {
        "cacheControl": null,
        "contentDisposition": null,
        "contentEncoding": null,
        "contentLanguage": null,
        "contentMd5": "-0q970378r08==",
        "contentType": "application/octet-stream"
      },
      "copy": {
        "completionTime": null,
        "id": null,
        "progress": null,
        "source": null,
        "status": null,
        "statusDescription": null
      },
      "creationTime": "2019-11-21T08:34:33+00:00",
      "deletedTime": null,
      "etag": "0237502375",
      "lastModified": "2019-11-21T08:34:33+00:00",
      "lease": {
        "duration": null,
        "state": "available",
        "status": "unlocked"
      },
      "pageBlobSequenceNumber": null,
      "remainingRetentionDays": null,
      "sequenceNumber": 1,
      "serverEncrypted": true
    },
    "snapshot": null
  },
  {
    "content": null,
    "deleted": false,
    "metadata": null,
    "name": "name/windows-2019-osDisk.f122fb3c-0edb-42a8-b98c-56657b447f15.vhd",
    "properties": {
      "appendBlobCommittedBlockCount": null,
      "blobTier": "P10",
      "blobTierChangeTime": null,
      "blobTierInferred": true,
      "blobType": "PageBlob",
      "contentLength": 136367309312,
      "contentRange": null,
      "contentSettings": {
        "cacheControl": null,
        "contentDisposition": null,
        "contentEncoding": null,
        "contentLanguage": null,
        "contentMd5": "0237502375/hKOg==",
        "contentType": "application/octet-stream"
      },
      "copy": {
        "completionTime": null,
        "id": null,
        "progress": null,
        "source": null,
        "status": null,
        "statusDescription": null
      },
      "creationTime": "2019-11-21T08:35:03+00:00",
      "deletedTime": null,
      "etag": "20397520i3h523",
      "lastModified": "2019-11-21T08:35:03+00:00",
      "lease": {
        "duration": null,
        "state": "available",
        "status": "unlocked"
      },
      "pageBlobSequenceNumber": null,
      "remainingRetentionDays": null,
      "sequenceNumber": 1,
      "serverEncrypted": true
    },
    "snapshot": null
  }
]

I sort this on creationTime in jq like so: jq 'sort_by(.properties.creationTime)' When I pipe this through | .[].name making jq 'sort_by(.properties.creationTime) | .[].name I get a sorted list of the names.

My question is: How can I pass an integer to this command and remove these many names, starting from the most recent created (bottom)?

Upvotes: 0

Views: 48

Answers (2)

Jan Myszkier
Jan Myszkier

Reputation: 2744

following https://stedolan.github.io/jq/manual/v1.6/#Builtinoperatorsandfunctions

slicing should work:

jq 'sort_by(.properties.creationTime) | .[-1:]'

Upvotes: 1

MyName
MyName

Reputation: 370

The answer I've used is the following:

jq 'sort_by(.properties.creationTime) | .[-4:] | .[].name'

Where -4 is the amount of names to display starting from the bottom.

Upvotes: 0

Related Questions