Sunday 16 May 2021

Jq: Select the object based on a property value

Using ‘select’ function, you can extract the object based on a property value.

 

emps.json

emps.json 
[{
		"id": 1,
		"firstName": "Ram",
		"lastName": "Gurram",
		"age": 23
	},
	{
		"id": 2,
		"firstName": "Sailaja",
		"lastName": "PTR",
		"age": 31
	},
	{
		"id": 3,
		"firstName": "Venkat",
		"lastName": "IT",
		"age": 30
	},
	{
		"id": 4,
		"firstName": "Gopi",
		"lastName": "Battu",
		"age": 34
	}
]

 

a. Get all the employees whose id is even number

$cat emps.json | jq '.[] | select(.id % 2 == 0)'
{
  "id": 2,
  "firstName": "Sailaja",
  "lastName": "PTR",
  "age": 31
}
{
  "id": 4,
  "firstName": "Gopi",
  "lastName": "Battu",
  "age": 34
}

 

b. Get all the employees whose firstName contains string ‘ai’

$cat emps.json | jq '.[] | select(.firstName | contains("ai"))'
{
  "id": 2,
  "firstName": "Sailaja",
  "lastName": "PTR",
  "age": 31
}

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment