Wednesday 5 May 2021

jq: -S: sort keys of objects on output

-S option is used to sort the keys of objects on jq output.

 

empDetails.json 

{"org":[{"name":"Honeywell","yrsOfExperience":2.2},{"name":"IBM","yrsOfExperience":1.8}],"firstName":"Krishna","lastName":"Ram"}

 

By default, jq command display json as it is.

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

 

If you want to sort the json output using the keys, use -S’ option.

$cat empDetails.json | jq -S
{
  "firstName": "Krishna",
  "lastName": "Ram",
  "org": [
    {
      "name": "Honeywell",
      "yrsOfExperience": 2.2
    },
    {
      "name": "IBM",
      "yrsOfExperience": 1.8
    }
  ]
}

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment