2.6 KiB
Certainly! For each file type—JSON, CSV, and YAML—let's identify the best tools for common use cases and provide a quick guide with syntax examples to get you started.
JSON: jq
Installation: Debian-based Linux:
sudo apt-get install jq
Common Use Case: Extracting Data
- Extract value(s) of a specific key:
jq '.key' file.json - Filter objects based on a condition:
jq '.[] | select(.key == "value")' file.json
Modifying Data:
- Modify a value:
jq '.key = "new_value"' file.json
Pretty-Printing:
- Format JSON file:
jq '.' file.json
CSV: csvkit
Installation: Debian-based Linux:
sudo apt-get install csvkit
Common Use Case: Analyzing Data
- Print CSV file with headers:
csvlook file.csv - Convert JSON to CSV:
in2csv file.json > file.csv
Filtering and Querying:
- Query CSV using SQL-like commands:
csvsql --query "SELECT column FROM file WHERE column='value'" file.csv
Combining and Exporting:
- Combine multiple CSV files:
csvstack file1.csv file2.csv > combined.csv
YAML: yq (Version 4.x)
Installation: Using pip:
pip install yq
Note: This also installs jq because yq is a wrapper around jq for YAML files.
Common Use Case: Extracting Data
- Extract value(s) of a specific key:
yq e '.key' file.yaml - Filter objects based on a condition:
yq e '.[] | select(.key == "value")' file.yaml
Modifying Data:
- Modify a value:
yq e '.key = "new_value"' -i file.yaml
Conversion to JSON:
- Convert YAML to JSON:
yq e -o=json file.yaml
Combining Tools in Workflows
- While
jqandyqcover JSON and YAML manipulation respectively,csvkitprovides a robust set of utilities for CSV files. These tools can be combined in workflows; for example, converting CSV to JSON withcsvkitand then manipulating the JSON withjq. - For Python developers, these command-line operations can complement the use of Python libraries like
json,csv, andPyYAML, allowing for quick data format conversions or manipulations directly from the terminal.
Summary
This guide presents a focused tool for each data format—jq for JSON, csvkit for CSV, and yq for YAML—along with basic syntax for common tasks like data extraction, modification, and format conversion. Integrating these tools into your development workflow can significantly enhance your productivity and data manipulation capabilities directly from the command line.