Using jq in Bash: Tips and Tricks

Jesus Sandoval
2 min readAug 11, 2023

jq is a powerful command-line JSON processor that's available on most platforms. Whether you're handling data for an API, configuration files, or any JSON data, jq can simplify and enhance your Bash scripting experience. In this article, we'll explore some tips and tricks that can help you get the most out of jq.

Photo by Daniel Dan on Unsplash

1. Basic JSON parsing

Start with understanding the basics. To parse and display a specific property from a JSON string, use the . (dot) notation.

echo '{"name":"John", "age":30, "city":"New York"}' | jq '.name'
# Output: "John"

2. Using filters

The power of jq lies in its ability to filter, map, and transform structured data.

echo '[{"name":"John", "age":30}, {"name":"Doe", "age":25}]' | jq '.[] | .name'
# Output:
# "John"
# "Doe"

3. Conditional Outputs

You can create conditional outputs based on values.

echo '{"name":"John", "age":30, "city":"New York"}' | jq 'if .age > 25 then "above 25" else "below or 25" end'
# Output: "above 25"

4. Modify Values

With jq, you can modify and add new fields to the JSON.

echo '{"name":"John", "age":30}' | jq '.age…

--

--