
JSON (JavaScript Object Notation) is a lightweight data-interchange format, which has the added benefit of being easy for humans to read and write. It is in common use and is easy for machines to parse and generate. It consists of key-value pairs, arrays, and nested objects, which together make JSON a versatile format for data storage and communication.
Parsing JSON data in Bash is useful for quick data manipulation and automation tasks, and is easily-integrated into Bash scripting. Let’s have a look at how we can parse JSON data using Bash.
Today we will be using:
- jq: A powerful command-line tool specifically designed for parsing, filtering, and transforming JSON data
Let’s have a look at basic jq syntax:
- .: Refers to the entire JSON document.
- .fieldName: Accesses a specific field
- .[]: Iterates over elements in an array
- |: Pipes data from one command to another
Basic examples of jq syntax usage:
# Outputs the entire JSON jq '.' data.json # Extracts a specific field jq '.fieldName' data.json
How to Parse JSON Data in Bash
To read JSON data from a file, you can use the cat command:
cat data.json
Use jq to extract specific fields from the JSON data:
jq '.fieldName' data.json
Access nested data using dot notation:
jq '.parentField.childField' data.json
Use jq filters to select and filter JSON data:
jq '.arrayField[] | select(.subField == "value")' data.json
Modify and update JSON data using jq:
jq '.fieldName = "newValue"' data.json
Combine pipes and multiple jq commands:
jq '.arrayField[] | {name: .name, value: .value}' data.json
A Few Practical Examples
Parsing API Responses
Fetch JSON data from an API using curl and parse it with jq:
curl -s 'https://api.example.com/data' | jq '.results'
Processing JSON Logs
Read and parse JSON formatted logs:
cat log.json | jq '.logEntries[] | {timestamp: .timestamp, message: .message}'
Final Thoughts
Keep exploring jq to master JSON parsing in Bash scripts, and understand these key factors to efficient JSON parsing:
- Understand the JSON structure beforehand to write efficient jq queries
- Use jq‘s filtering capabilities to minimize the data processed
Check the following resources for additional information on writing Bash scripts:
- How to Automate Data Collection with Bash Scripts
- Tips for Writing Awesome Bash Scripts
- 5 Interesting Things to do with Bash Scripts
Happy bashing!
