Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract element based on where clause in jq json

Tags:

json

jq

My Json structure is like below.

How can i extract id of project whose name is ansible using jq?

The json file is very big

https://stedolan.github.io/jq/manual/

  {
    "id": 38,
    "name": "ansible",
    "path": "ansible",
    "description": "Ansible playbooks, roles, and supporting tools repos"
  },
  {
    "id": 18,
    "name": "Analytics",
    "path": "analytics",
    "description": "Big Data Analytics projects"
  }
like image 526
user2230605 Avatar asked Dec 19 '22 15:12

user2230605


1 Answers

Assuming your input is actually an array of those objects, you could do this:

$ jq --arg name "ansible" '.[] | select(.name == $name).id' input.json
like image 177
Jeff Mercado Avatar answered Jan 11 '23 07:01

Jeff Mercado