How to filter an array of objects based on values in an inner array with jq?

Cover Image for How to filter an array of objects based on values in an inner array with jq?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

How to filter an array of objects based on values in an inner array with jq?

Are you struggling to filter an array of objects in jq based on values in an inner array? Fret not! In this blog post, we'll explore a common issue and provide you with easy, step-by-step solutions to help you tackle this problem. 🧩

The Problem

Let's start by examining the specific problem at hand. We have an array of objects with the following structure:

[
  {
    "Id": "cb94e7a4...",
    "Names": [
      "condescending_jones",
      "loving_hoover"
    ]
  },
  {
    "Id": "186db739...",
    "Names": [
      "foo_data"
    ]
  },
  {
    "Id": "a4b7e6f5...",
    "Names": [
      "jovial_wozniak"
    ]
  },
  {
    "Id": "76b71c49...",
    "Names": [
      "bar_data"
    ]
  }
]

Our goal is to construct a filter using jq that returns all objects whose Id does not contain the string "data" in the inner Names array. The expected output, when applying the filter to the provided input, would be:

cb94e7a4...
a4b7e6f5...

The Solution

To achieve the desired filtering, we need to correctly use the select filter in jq. 🕵️‍♀️

Here's a step-by-step solution:

  1. Start by applying the select filter to each object in the array. We want to select objects where none of the names in the Names array contain the string "data". The filter below accomplishes this:

    select( any(.Names[]; contains("data")|not) )
  2. Next, we want to extract only the Id value from the selected objects. We can use the map function along with the .Id selector:

    map(.Id)
  3. Finally, we want to join the resulting array of Id values into a newline-separated string. You can achieve this using the join function with the newline character as the separator:

    join("\n")

Putting it all together, the complete jq filter would look like this:

[
  {
    "Id": "cb94e7a4...",
    "Names": [
      "condescending_jones",
      "loving_hoover"
    ]
  },
  {
    "Id": "186db739...",
    "Names": [
      "foo_data"
    ]
  },
  {
    "Id": "a4b7e6f5...",
    "Names": [
      "jovial_wozniak"
    ]
  },
  {
    "Id": "76b71c49...",
    "Names": [
      "bar_data"
    ]
  }
]
| select( any(.Names[]; contains("data")|not) )
| map(.Id)
| join("\n")

When you run this filter against the provided input, you'll get the desired output:

cb94e7a4...
a4b7e6f5...

Conclusion

Filtering an array of objects based on values in an inner array can be a tricky task, but with the right approach, it becomes a breeze. In this guide, we've shown you how to use jq to accomplish this task step-by-step. Remember, applying the select filter, extracting the desired values, and joining them appropriately are key.

Now it's your turn! Give it a try with your own data and let us know how it goes. If you encounter any difficulties or have any questions, feel free to leave a comment below. Happy filtering! 🎉😊


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello