• Uncategorized

About json : Json-response-data-extraction-using-linux-command

Question Detail

Below is the json response of an api endpoint, I wanted to get the id value of specific name in the below array of json response.

{
  "self": "https://testtoo.atlassian.net/rest/api/3/project/TEST/version?maxResults=50&startAt=0",
  "maxResults": 50,
  "startAt": 0,
  "total": 2,
  "isLast": true,
  "values": [
    {
      "self": "https://testtoo.atlassian.net/rest/api/3/version/10001",
      "id": "10001",
      "description": "test release",
      "name": "test2",
      "archived": false,
      "released": true,
      "releaseDate": "2021-12-29",
      "userReleaseDate": "29/Dec/21",
      "projectId": 10000
    },
    {
      "self": "https://testtoo.atlassian.net/rest/api/3/version/10002",
      "id": "10002",
      "name": "test3",
      "archived": false,
      "released": true,
      "projectId": 10000
    }
  ]
}

eg: I wanted to get id value(10002) of name test3, with the help of name key. Using jq I’m able to select the name key, but I’m not to find a way to get previous key value(id) using name key! My requirement is to get id value by inputting the corresponding name value.

Question Answer

Try

jq -r --arg name "test3" '.values[] | select(.name== $name).id'
10002

Demo

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.