Thursday, 6 May 2021

jq: . : Access value of specific property

Using . operator, we can access specific value of a property.

 

empPretty.json 

{
  "org": [{
    "name": "Honeywell",
    "yrsOfExperience": 2.2
  }, {
    "name": "IBM",
    "yrsOfExperience": 1.8
  }],
  "firstName": "Krishna",
  "lastName": "Ram",
  "address": {
    "city": "Bangalore",
    "country": "India"
  }
}

 

.org: access the value of org property

.firstName: access the value of firstName property

.lastName: access the value of lastName property

.address.city access the value of city property.

 

$cat empPretty.json | jq '.org'
[
  {
    "name": "Honeywell",
    "yrsOfExperience": 2.2
  },
  {
    "name": "IBM",
    "yrsOfExperience": 1.8
  }
]
$
$
$cat empPretty.json | jq '.firstName'
"Krishna"
$
$cat empPretty.json | jq '.lastName'
"Ram"
$
$cat empPretty.json | jq '.address.city'
"Bangalore"

 

Access multiple properties in single command

Using , operator, you can access multiple properties in single command.

 

Example

$cat empPretty.json | jq '.firstName, .lastName'
"Krishna"
"Ram"

Above command extract firstName and lastName properties.

$cat empPretty.json | jq '.org[].name, .org[].yrsOfExperience'
"Honeywell"
"IBM"
2.2
1.8


Above command extract organization name and yrsOfExperience of an employee.



Previous                                                    Next                                                    Home

No comments:

Post a Comment