what is .. (two dots) in jq filter

2 min read 05-10-2024
what is .. (two dots) in jq filter


Demystifying the Double Dot (..) in jq Filters

jq, the powerful JSON processor, offers a plethora of tools for manipulating and extracting data from JSON documents. One such tool, the double dot (..), often leaves new users puzzled. This article will shed light on the functionality of the double dot, explaining its purpose and how it empowers your jq queries.

The Scenario: Unraveling Nested JSON Data

Imagine you have a JSON document representing a collection of books, each with details like title, author, and a list of reviews. You want to extract the text of all reviews, no matter how deeply nested they are within the document. Here's a simplified example:

{
  "books": [
    {
      "title": "The Hitchhiker's Guide to the Galaxy",
      "author": "Douglas Adams",
      "reviews": [
        { "text": "A hilarious and thought-provoking classic!" },
        { "text": "Highly recommended for sci-fi fans." }
      ]
    },
    {
      "title": "The Lord of the Rings",
      "author": "J.R.R. Tolkien",
      "reviews": [
        { "text": "An epic fantasy masterpiece." },
        { "text": "A timeless tale of courage and fellowship." }
      ]
    }
  ]
}

The Solution: The Power of ..

The double dot (..) in jq is a recursive descent operator. It traverses the entire JSON document, including all nested objects and arrays, to find and process the desired data.

Here's how you can extract all the review texts using ..:

jq '.books[]..text' input.json

This query works as follows:

  1. .books[]: This selects all elements within the "books" array.
  2. ..: This recursively descends into each book object and its children, including the "reviews" array.
  3. .text: This finally extracts the "text" field within each review object.

The output will be a list of all review texts:

"A hilarious and thought-provoking classic!"
"Highly recommended for sci-fi fans."
"An epic fantasy masterpiece."
"A timeless tale of courage and fellowship."

Why Use ..?

  • Flexibility: .. allows you to extract data from any level of nesting within your JSON. This is crucial when dealing with complex data structures.
  • Simplicity: It provides a concise way to express recursive queries, making your code easier to read and understand.

Additional Insights:

  • Combining .. with Filters: You can combine .. with other jq filters to further refine your queries. For example, to extract review texts from books with a specific author, you can use:

    jq '.books[] | select(.author == "Douglas Adams") ..text' input.json
    
  • Specificity: If you want to extract data from only a certain level of nesting, you can use . (single dot) for immediate child access and [] for array access.

Conclusion:

The double dot operator in jq provides a powerful and efficient way to navigate through complex JSON structures. Mastering .. will significantly enhance your ability to extract and manipulate data from JSON documents.

Further Learning: