jq is a lightweight and flexible command-line JSON processor. It’s used to parse, filter, and transform JSON data. Instead of writing complex scripts to handle JSON, jq provides a concise and expressive query language to manipulate JSON structures easily. Think of it like grep or sed but specifically designed for JSON. You give it JSON data as input (from a file, a web API, or standard input), and it uses your query to output modified or filtered JSON. Here’s what makes jq powerful:

  • Simple Syntax: Its query language is relatively easy to learn, even for those unfamiliar with formal query languages.
  • Powerful Filtering: You can select specific parts of a JSON document with ease, based on keys, values, and nested structures.
  • Transformations: You can reshape JSON data, creating new structures from existing ones, changing data types, and more.
  • Programmatic Use: jq can be easily integrated into shell scripts and other programs.
  • Stream Processing: jq can handle large JSON documents efficiently, processing them piece by piece.

Example: Let’s say you have a JSON file data.json containing:

{ "name": "John Doe", "age": 30, "city": "New York" }

To extract the name, you’d use:

jq '.name' data.json

This would output:

"John Doe"

To get both the name and age:

jq '{name: .name, age: .age}' data.json

This outputs:

{ "name": "John Doe", "age": 30 }

jq is incredibly versatile and can handle much more complex scenarios, including arrays, nested objects, and conditional logic within its queries. It’s a valuable tool for anyone working with JSON data.


[
 
{ "book_title": "Wares Wally", "genre": "children", "page_count": 20 },
 
{ "book_title": "Charlottes Web Crawler", "genre": "young_ware", "page_count": 120 },
 
{ "book_title": "Charlie and the 8 Bit Factory", "genre": "young_ware", "page_count": 108 },
 
{ "book_title": "The Princess and the Pcap", "genre": "children", "page_count": 48 },
 
{ "book_title": "The Lion, the Glitch and the Wardrobe", "genre": "young_ware", "page_count": 218 }
 
]

How to filter only the book_title1 in above JSON using JQ?

neo@Neo:~$ jq '.[] | {book_title}' test.json
{
 "book_title": "Wares Wally"
}
{
 "book_title": "Charlottes Web Crawler"
}
{
 "book_title": "Charlie and the 8 Bit Factory"
}
{
 "book_title": "The Princess and the Pcap"
}
{
 "book_title": "The Lion, the Glitch and the Wardrobe"
}