jq

About JQ

JQ

jq is a library written in C and produces functionalities like sed for JSON data.
JSON is getting popular and used in various applications and many a time, developers or applications have to parse or read through JSON data or files.
JQ comes very handy in these cases.

jq

Let’s install it

We can install using below commands
Linux: sudo apt-get install jq
OSX: brew install jq
Windows: chocolatey install jq

 

 

Let’s see if it is installed

Run below command, and it should show the version details

jq –version
jq-1.5

 

 

Lets see jq in action:

 

Print a simple json data

sample='[{“firstname”:”john”},{“lastname”:”dow”}]’
echo “${sample}” | jq ‘.’

Output:
[
{
“firstname”: “john”
},
{
“lastname”: “dow”
}
]

echo “${sample}” | jq ‘.[0].firstname’
Output
john

Lets create a JSON file.
create data.json with below contents
[
{
“Data”: {
“Name”: “test”,
“DB”: {
“first”: {
“url”: “http://test…./123”,
“port”: “5123”
},
“second”: {
“url”: “http://test…./124”,
“port”: “5122”
}
}

}
}
]

Now lets parse it

echo jq ‘.[] | .Data.DB.first.url’ data.json
Output:
“http://test…./123”

So we can parse json data or files and extract information/data that is required.

Please refer jq main page for more details https://stedolan.github.io/jq/

 

Leave a Comment

Your email address will not be published. Required fields are marked *