TIL 2

Posted on Sep 25, 2021

jq command

A command to manipulate json files. At work we usually have a bunch json serialized ouputs in S3 or some other datastore, which are absurdly many lines long and deeply nested. Often, if I need to grep something out of it, I’ll just copy and paste it to a company internal json prettifier to make it human readable and get what I need. There must be some out on the web too. Something like this.

It’s a hassle when you need to switch multiple windows, download files, copy paste content, etc. Now come ‘jq’, a command line utility to, hopefully, end all that.

Download(on mac): brew install jq

Print full json:

>> jq '.' your_file.json

Print a level-1 keys value:

>> jq '.your_key' your_file.json

Print a level-2 keys value:

>> jq '.your_key.your_sub_key' your_file.json   # and so on

Print an array element (0 indexed):

>> jq '.[4]' your_file.json     # index 4 is the 5th element in a 0 indexed array

Print a range of array elements:

>> jq '.[2:6]' your_file.json   # prints index 2 up until 5

Print range without bound:

>> jq '.[2:'] your_file.json    # prints index 2 till the last

Print range from the right end:

>> jq '.[-3:]' your_file.json   # prints the last 3 elements

Print key of an array element:

>> jq '.[4].key' your_file.json # prints the index 4's key

Print for all array elements key:

>> jq '.[].key' your_file.json  # for all elements in array having 'key', all its values will get printed

There so much more power in jq, feel free to explore.

References: