Codementor Events

JQ: Processing JSON on the fly from terminal

Published Feb 19, 2021
JQ:  Processing JSON on the fly from terminal

Have you ever got a big JSON file from product managers, to analyze the data on various basic pivots, and you start opening your IDE and ready for the script to parse JSON and output some result on applying some basic operations like: select, filter, groupby, etc......

Wait isn't this a repeated work and shouldn't this problem be already solved. Yeah jq rescues us from this situation and let us analyze JSON data from terminal only. JQ works as sed or awk for json files.Awesome !!!

Let's start with some real JSON. Consider the following JSON to be analyzed (data.json):

{
  "type": "message",
    "user": "Dan",
    "text": "Hi Terry", 
    "profile": [{"designation":"developer", "experinece": 4}]
}
{
    "type": "message",
    "user": "Terry",
    "text": "Heyaa",
    "profile": [{"designation":"manager", "experinece": 10}]
}

Ready for starting playing with the above json:
1: Lets print the above file as one json in a line

cat data.json | jq

Screenshot 2021-02-17 at 11.04.10 PM.png

So cool !!!

2: Let's fetch out all the "text" values from the data

jq -c '.text' data.json
Screenshot 2021-02-17 at 11.05.54 PM.png

3: Let's fetch records where user == 'Terry'

jq -c 'select(.user == "Terry")' data.json
Screenshot 2021-02-17 at 11.09.12 PM.png

4: Let's try some operation on nested JSON. Let's fetch all the records of managers

jq -c 'select(.profile | .[] | .designation == "manager")' data.json
Screenshot 2021-02-17 at 11.08.10 PM.png

Cool.. Right!!

That's not all. This blog was just a small intro to a powerful tool jq.

Even you can get your hands dirty with syntax of jq on jqplay.org

Go through this page for installation and just start on your terminal !!

Stay tuned for more such hidden yet useful tools..

Discover and read more posts from Vivek
get started
post commentsBe the first to share your opinion
Show more replies