Charles Green
Charles Green

Reputation: 423

How do I return the id associated with the test json element?

I return the following json from a curl. I've simplified the ID's however my goal is to return the id related to the test object. The desired return value is 55555.

I'm using jq to parse the string. https://stedolan.github.io/jq

[
  {
    "attributes": null,
    "id": "44444",
    "name": "production",
    "type": "system_group"
  },
  {
    "attributes": null,
    "id": "55555",
    "name": "test",
    "type": "system_group"
  },
  {
    "attributes": null,
    "id": "66666",
    "name": "uat",
    "type": "system_group"
  }
]

Upvotes: 1

Views: 37

Answers (1)

Inian
Inian

Reputation: 85825

It should be pretty straightforward filter, use the select and contain constructs.

jq '.[] | select( .name| contains("test")) | .id'

Call the filter with -r to remove the quotes and print the raw value.

jqplay-URL

Upvotes: 1

Related Questions