JSON Manipulation with jq Filter – Examples

In article, I covered techniques of JSON manipulation with jq filter, a powerful command-line tool designed for processing JSON data. I demonstrated how to apply various filters and transformations to JSON structures, showcasing the flexibility and efficiency of jq in handling complex data formats. Through practical examples, I illustrated how jq can be effectively used to extract, modify, and restructure JSON data, making it an invaluable tool for data manipulation tasks. you’ll learn how to use different jq filters such as prettify, filters on arrays, objects, and more complex operations involving select(), map(), and regular expressions.

JSON manipulation with jq filter

Table of Contents

 

1. Prettify

The simplest jq filter is ‘.’ which prints the input as it is, but adding the ‘-C’ flag can prettify the output with colors.

jq -C '.' sample.json

Sample JSON input:

{"name": "jq tutorial", "version": "1.0"}

Output:

{
  "name": "jq tutorial",
  "version": "1.0"
}

 

2. Array Filters

To filter an array and return its elements, you can use the ‘[.[]]’ syntax.

jq '.[]' sample.json

Sample JSON input:

["jq tutorial", 1.0]

Output:

"jq tutorial"
1.0

 

3. Object Filters

To filter and retrieve a specific property from an object, use ‘.property_name’.

jq '.name' sample.json

Sample JSON input:

{"name": "jq tutorial", "type": "filter"}

Output:

"jq tutorial"

 

4. Array of Objects

To get a list of values from an array of objects, you can use ‘.[] | .property_name’.

jq '.[] | .name' sample.json

Sample JSON input:

[{"name": "jq tutorial"}, {"name": "filter guide"}]

Output:

"jq tutorial"
"filter guide"

 

5. Nested Array

To navigate through a nested array, combine array filters and object filters.

jq '.[][]' sample.json

Sample JSON input:

[[1,2],[3,4]]

Output:

1
2
3
4

 

6. Advanced Filters

For more complex data manipulation, combine and chain jq filters as needed.

jq '.users[] | select(.age > 21).name' sample.json

Sample JSON input:

{"users": [{"name": "Alice", "age": 22}, {"name": "Bob", "age": 20}]}

Output:

"Alice"

 

7. The select() Function

The select() function is crucial for filtering arrays and objects based on conditions.

jq '.users[] | select(.active)' sample.json

Sample JSON input:

{"users": [{"name": "Alice", "active": true}, {"name": "Bob", "active": false}]}

Output:

{"name": "Alice", "active": true}

 

8. The map() Function

Use the map() function to apply a filter to each element of an array.

jq 'map(. + 1)' sample.json

Sample JSON input:

[1, 2, 3]

Output:

[2, 3, 4]

 

9. Slicing Arrays and Strings

To slice arrays or strings, use the syntax ‘start:end’.

jq '.[1:3]' sample.json

Sample JSON input:

["a", "b", "c", "d"]

Output:

["b", "c"]

 

10. Regular Expressions

For pattern matching using regular expressions, use the ‘test()’ function.

jq 'select(.name | test("^(jq)"))' sample.json

Sample JSON input:

[{"name": "jq tutorial"}, {"name": "regex guide"}]

Output:

{"name": "jq tutorial"}

Summary

In this tutorial, we covered various jq filters to jq filter json data, including prettification, filtering arrays and objects, using select() and map() for conditional selection and transformation, and slicing and regex for precise data extraction. With these techniques, you’re well-equipped to manipulate and query any JSON data effectively.

References