dks551
dks551

Reputation: 1113

Extract inner field value from JSON document based on a key using jq

I have a json file with the below format. I want to extract the name field value from inside based on the id value. so, if I pass id value as 99900, it should return me DEF. How can I do that using jq.

{
  "11213-99900": {
    "cid": "11213-99900",
    "name": "DEF",
    "id": "99900"
  },
  "11213-12345": {
    "cid": "11213-12345",
    "name": "ABC",
    "id": "12345"
  },
  "11272-23456": {
    "cid": "11272-23456",
    "name": "YXX",
    "id": "23456"
  }
}

Upvotes: 0

Views: 63

Answers (1)

oguz ismail
oguz ismail

Reputation: 50805

Use select:

$ jq -r --arg q 99900 '.[] | select(.id == $q).name' file
DEF

Upvotes: 3

Related Questions