• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
NameHero® Blog

NameHero® Blog

Web Hosting Tips & Resources From NameHero

  • AI Agents
    • OpenClaw
    • Claude Code
    • n8n
    • Hermes Agent
    • Open WebUI
    • Docker
  • Hosting
    • Web Hosting
    • WordPress Hosting
    • WooCommerce Hosting
    • Enterprise Hosting
    • Email Hosting
    • HeroicGuard
    • GPU Hosting
    • Website Builder
  • VPS
    • Managed VPS
    • Unmanaged VPS
    • Flex VPS
  • Reseller
  • Gaming
  • Domains
  • Account
  • Blog Home
  • Categories
  • Authors

Jq Command: All you Need to Know

Bhagwad Park

Published on: August 6, 2025

Categories: Linux Command Line, VPS Hosting 0

The “jq” command is a built-in tool in Linux that allows us to process JSON files with the same precision and flexibility that a tool like “awk” or “sed” does with text. We’ve seen in earlier tutorials how powerful “awk” can be, and “jq” brings a similar functionality to JSON. JSON is becoming more and more popular these days in a variety of applications. It’s the most flexible data interface standard between disparate applications, making it a powerful currency when systems need to communicate with one another. Even applications that don’t use JSON usually export it when they need to communicate with an external service. So knowing how to use jq is a valuable skill.

Let’s get started.

Table of Contents
  • Basic jq Format
    • Displaying the Entire JSON File
    • Show Each Element Separately
    • Accessing the Value of Specific Keys in a Series of Objects
    • Filtering Values Based on a Condition
    • If-Then-Else Statements
    • Extracting JSON Elements with map(filter)
  • Conclusion

Basic jq Format

The jq command can have a confusing syntax, so let’s take it step by step. Let’s assume we have a simple JSON file like this, saved in a file called “users.json”.

[
{
"name": "Alice",
"age": 30,
"city": "Toronto",
"temp_c": 18.5
},
{
"name": "Bob",
"age": 25,
"city": "Vancouver",
"temp_c": 12.3
}
]

The above JSON file contains a single array with two objects. These objects contain their own key/value pairs.

Displaying the Entire JSON File

Let’s say you want to display the entire JSON file. You’d use this command:

jq '.' users.json

This gives the following output:

Basic jq Output
Basic jq Output

You can see that, by default, the jq command formats its output so you can easily see the various fields and their values. Unlike with the “cat” command, which just shows everything in plain text:

Cat Command with JSON Text
Cat Command with JSON Text

So far, not super impressive. Let’s see what more we can do.

Show Each Element Separately

If you look at the output of the above two screenshots, you’ll see that the entire thing is bound by two square brackets:

[
{…}
{…}
]

This is because in the above example, the JSON file is just one big array containing several objects, and that’s what the output shows. But using the jq command, we can also show each object separately like this:

jq '.[]' users.json

This gives the following output:

Showing Objects of the JSON File
Showing Objects of the JSON File

Here, the “.[]” syntax has iterated over each object in the larger array and is showing us the values. Note the absence of the opening and closing square brackets at the beginning at the end. The output is just this:

{…}
{…}

Now this might not seem like a big deal, but it’s the foundation for future users of jq. Once we have a method like “.[]” to iterate over the values of a table-like structure, then we can access the data contained in each of those values.

Accessing the Value of Specific Keys in a Series of Objects

Now, let’s say we want to extract the names in the JSON file. We have an array containing two objects, each of which has a “name” field. The command to extract the names is:

jq '.[].name' users.json

This gives us the following output:

Accessing the Values of Each Object
Accessing the Values of Each Object

Now we see the value of the “.[]” syntax. Because it gave us a series of objects inside the array, we can access the “name” field via “.name” after it. If instead, we had tried to access the names without iterating over the objects inside the array, like this:

jq '.name' users.json

We would get an error:

Error Cannot Index Array
Error Cannot Index Array

This is because we’re trying to perform an incompatible operation. There is no “name” key for the single array of objects in the JSON file. That key exists only for the objects inside the array, so we need to list them all first with “.[]” and then use the “.name” expression to get the values of the names.

Filtering Values Based on a Condition

The above two commands allow you to display all the records of the JSON file, even if you’re only looking at a single field, as with the ” jq ‘.[].name’ users.json” command. But the jq command also lets you display only those fields that match specific criteria. For example, let’s say we want to only display those entries in our JSON file where the age of the person is greater than 28. The command to achieve that would be:

jq '.[] | select(.age > 28)' users.json

As shown here:

Over the age of 28

Here, there’s only one entity with an age greater than 28, so only one record is displayed. The general syntax for the “select” statement is:

select(condition)

Here, “condition” is any statement that evaluates to true or false. The confusing part is that the syntax for these conditions isn’t the standard bash syntax. As we’ve seen in the bash string comparison guide, bash can have strange operators for comparing numbers like “gt”, depending on the context. It also uses “&&”, “||”, and “!” for logical operators.

Jq simply uses standard arithmetical operators like “>” for “greater than”, and its logical operators are “and”, “or”, and “not”. The following are some examples of conditions in jq:

jq '.[] | select(.age > 25 and .city == "Toronto")' users.json

The above statement will check for two conditions simultaneously.

jq '.[] | select(.age < 26 or .city == "Toronto")' users.json

This statement will check to see if either of the two conditions is met

jq '.[] | select(.city != "Toronto")' users.json

This will check to see if a condition doesn’t evaluate to true

Jq also has several string-matching functions that let you quickly evaluate strings. For example:

jq '.[] | select(.name | startswith("A"))' users.json

This will select those records where the name starts with “A”. Other string functions are:

endswith(string)
contains(string)

If-Then-Else Statements

We can extend jq’s functionality even further. By incorporating if-then-else statements, we can choose to display values in a certain way. So far, we’ve always displayed the entire record whenever a condition evaluates to be “true”. But using the if-then-else statement, we can choose to display only a single field. For example:

jq '.[] | if .age > 28 then "Adult: \(.name)" else "Young: \(.name)" end' -r users.json

Here you can see that if the age is greater than 28, then we choose to display only the name of the person with a prefix of “Adult” or “Young”. Here’s the output:

Display only the Name

For this, you can see that the if-then-else statement can be used to format the contents of a JSON file and present the results in a human-readable manner. It’s very useful while parsing the contents of a long JSON file to extract the information.

Extracting JSON Elements with map(filter)

Finally, jq allows us to filter records in such a way that the output contains only an array of the remaining elements. This uses the “map” syntax. While kind of similar to ” .[].select(…)”, the “map” command lets you create a new JSON file from an existing one. For example:

jq 'map(select(.age > 28))' users.json

This returns only those records in users.json where the age is greater than 28, but as a JSON file in its own right. Here’s the output:

JSON Map Command

You can parse this output into a file of your own, and it’ll be a regular JSON file. You can even choose the records you want to include in the new file:

jq 'map({name: .name, city: .city})' users.json

This will extract only the name and city fields of the original JSON, like this:

Return only the City and Name

And we can even combine the two:

jq 'map(select(.age > 28) | {name: .name, city: .city})' users.json

Giving:

Combine Filter and Transform
Combine Filter and Transform

This lets you transform JSON files easily using flexible criteria.

Conclusion

JSON files are widely used for standard data interoperability. Using the jq command, we can manipulate JSON so that we extract only those parts of it that are valid for our needs. With complex filtering and conditional criteria, jq is a Swiss army knife for JSON files!

Bhagwad Park Profile Picture
Bhagwad Park

I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!

Related Posts

How to Use Tmux: The Complete Guide

Tmux is a "terminal multiplexer", that allows you to run several processes from one command line window. Here's how it works.

All About Bash Parameter Expansion

Parameter expansion in bash is very useful when working with strings like filenames and log entries. It allows you to clean up text.

How to Use the Tee Command in Linux

The tee command in Linux is useful when you want to direct the output of a command to multiple locations. Here's how to use it.

Docker Build Cache – How it Works and Invalidating it

Docker heavily optimizes its build process with caching to save time. But it's important to know how to invalidate it.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow & Subscribe

Exclusive promos, content and more!


Most Popular Posts

NameHero’s Recommended WordPress Plugin and Theme Setup

WordPress Hosting vs. Web Hosting – What’s The Difference?

How To Increase The InnoDB Buffer Pool Size

How To Fix A Stuck All-in-One WP Migration Import

How To Add A Subdomain In Cloudflare

Top Categories

  • WordPress
  • WordPress Tutorials
  • OpenClaw Hosting
  • Enterprise Hosting
  • WooCommerce
  • Web Hosting
  • Resellers
  • Website Security
  • Website Development
  • Website Performance
  • VPS Hosting
  • SEO Tips
  • Announcements
  • Domain Registration
NameHero

NameHero® proudly provides web hosting to over 40,000 customers with 99.9% uptime to over 750,000 websites.

  • Master Card
  • Visa
  • American Express
  • Discover
  • Paypal
Products
  • Web Hosting
  • Managed VPS Hosting
  • Unmanaged VPS Hosting
  • Flex VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • GPU Hosting
  • Email Hosting
  • HeroicGuard
  • Domains
  • Website Builder
  • AI Agent Hosting
Help & Support
  • NameHero Blog
  • NameHero Gaming Blog
  • Support
  • Help Center
  • Migrations
  • Affiliates
  • Gaming Affiliates
  • Call 1-855-984-6263
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2026 Name Hero, LLC. All rights reserved.
NameHero® is a registered trademark.

  • Privacy Policy
  • Terms of Use
  • Acceptable Use Policy
  • Payment Policy
  • DMCA