Tuesday 4 May 2021

jq: Pretty print json

In general, we can invoke ‘jq’ command by providing a piece of JSON to its standard input.

 

For example,

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

 

Above command pass the output of echo as input to ‘jq’ command. 

$echo '{"org":[{"name":"Honeywell","yrsOfExperience":2.2},{"name":"IBM","yrsOfExperience":1.8}],"firstName":"Krishna","lastName":"Ram"}' | jq 
{
  "org": [
    {
      "name": "Honeywell",
      "yrsOfExperience": 2.2
    },
    {
      "name": "IBM",
      "yrsOfExperience": 1.8
    }
  ],
  "firstName": "Krishna",
  "lastName": "Ram"
}

 

As you see the output, jq command formats the json.

 

You can even keep the json in a file and pass it as input to jq command.

 

empDetails.json

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

 

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

 

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment