jq Command Examples

“jq” is a versatile and powerful command-line tool used for processing and manipulating JSON data. JSON (JavaScript Object Notation) is a lightweight data-interchange format commonly used for representing structured data. “jq” provides a domain-specific language (DSL) specifically designed for querying, filtering, transforming, and formatting JSON data efficiently. Here’s a more detailed explanation of its features and functionalities:

  • Querying and Filtering: One of the primary functions of “jq” is to query and filter JSON data to extract specific elements or subsets of the data. It allows users to navigate through JSON objects and arrays using selectors and filters, enabling precise extraction of data based on criteria such as key names, attribute values, or array indices.
  • Transformation and Modification: “jq” facilitates the transformation and modification of JSON data by applying various operations and functions to manipulate the structure and content of JSON objects and arrays. Users can perform tasks such as adding or removing elements, modifying values, merging objects, flattening nested structures, and restructuring data according to specific requirements.
  • Formatting and Pretty-Printing: “jq” offers features for formatting and pretty-printing JSON data to improve readability and presentation. It provides options to indent and align JSON objects and arrays, making it easier for users to visually inspect and understand the structure of complex JSON documents.
  • Aggregation and Grouping: “jq” supports aggregation and grouping operations for summarizing and analyzing JSON data. Users can aggregate values, calculate statistics, and group data based on common attributes or keys, facilitating data summarization and analysis tasks.
  • Conditional Logic and Control Flow: “jq” includes support for conditional logic and control flow constructs, allowing users to implement conditional expressions, loops, and branching logic within JSON processing pipelines. This enables complex data processing workflows and allows for dynamic handling of JSON data based on specific conditions or criteria.
  • Composition and Pipelines: “jq” supports composition and chaining of operations through pipelines, allowing users to combine multiple queries, filters, and transformations into a single coherent workflow. This enables users to perform complex data processing tasks efficiently and expressively by breaking them down into smaller, composable components.
  • Integration with Command-Line Tools: “jq” seamlessly integrates with other command-line tools and utilities through pipes and redirection, enabling users to incorporate JSON processing into shell scripts, pipelines, and automation workflows. This interoperability enhances the versatility and utility of “jq” in various system administration, data processing, and automation tasks.
  • Extensibility and Customization: “jq” is highly extensible and customizable, allowing users to define custom functions, filters, and modules to extend its functionality and adapt it to specific use cases and requirements. This extensibility enhances the flexibility and power of “jq” for handling diverse JSON processing tasks.

jq Command Examples

1. Execute a specific expression (print a colored and formatted json):

# cat [path/to/file.json] | jq '.'

2. Execute a specific script:

# cat [path/to/file.json] | jq --from-file [path/to/script.jq]

3. Pass specific arguments:

# cat [path/to/file.json] | jq [--arg "name1" "value1" --arg "name2" "value2" ...] '[. + $ARGS.named]'

4. Print specific keys:

# cat [path/to/file.json] | jq '[.key1, .key2, ...]'

5. Print specific array items:

# cat [path/to/file.json] | jq '[.[index1], .[index2],...]'

6. Print all array items/object keys:

# cat [path/to/file.json] | jq '.[]'

7. Add/remove specific keys:

# cat [path/to/file.json] | jq '. [+|-] [{"key1": "value1", "key2": "value2", ...]}'

Summary

In summary, “jq” is a versatile and feature-rich command-line JSON processor that provides powerful capabilities for querying, filtering, transforming, and formatting JSON data. Its domain-specific language, support for aggregation and grouping, conditional logic, composition and pipelines, integration with command-line tools, and extensibility make it a valuable tool for developers, system administrators, and data professionals working with JSON data in various contexts.

Related Post