Saturday, 8 May 2021

jq: []: Navigate array

[] operator is used to navigate arrays.

 

empPretty.json 

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

 

For example,

.org[0].name gives "Honeywell",

.org[1]. yrsOfExperience gives 1.8.

$cat empPretty.json | jq '.org[0]'
{
  "name": "Honeywell",
  "yrsOfExperience": 2.2
}
$
$cat empPretty.json | jq '.org[0].name'
"Honeywell"
$
$cat empPretty.json | jq '.org[1]'
{
  "name": "IBM",
  "yrsOfExperience": 1.8
}
$
$cat empPretty.json | jq '.org[1].yrsOfExperience'
1.8

 

How to get all the organization names that employee worked?

Simply skip the array index parameter.

 

Example

.org[].name

 

Above snippet return all the organization names.

$cat empPretty.json | jq '.org[].name'
"Honeywell"
"IBM"

 

 

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment