Thursday, 13 May 2021

Jq: Get all the firstnames of employees from json document

 

compOperators.json 

{
	"1": {
		"org": [{
			"name": "ABC Cord",
			"yrsOfExperience": 2.2
		}, {
			"name": "XYZ",
			"yrsOfExperience": 1.8
		}],
		"firstName": "Krishna",
		"lastName": "Ram",
		"address": {
			"city": "Bangalore",
			"country": "India"
		}
	},

	"2": {
		"org": [{
			"name": "XYZ",
			"yrsOfExperience": 4.2
		}, {
			"name": "Bokara",
			"yrsOfExperience": 3.8
		}],
		"firstName": "Battu",
		"lastName": "Gopi",
		"address": {
			"city": "Bangalore",
			"country": "India"
		}
	},

	"3": {
		"org": [{
			"name": "Dongle",
			"yrsOfExperience": 2.6
		}, {
			"name": "Binare",
			"yrsOfExperience": 1.11
		}],
		"firstName": "Krishna",
		"lastName": "Ram",
		"address": {
			"city": "Hyderabad",
			"country": "India"
		}
	}

}

 

Get all the employees firstNames

 

$cat compOperators.json | jq '.[].firstName'
"Krishna"
"Battu"
"Krishna"


Get all the employees firstName and lastName

$cat compOperators.json | jq '.[] | .firstName + "," + .lastName'
"Krishna,Ram"
"Battu,Gopi"
"Krishna,Ram"



Previous                                                    Next                                                    Home

No comments:

Post a Comment