Use jq for JSON Processing
jq is a powerful command-line JSON processor that allows you to parse, filter, and manipulate JSON data.
Sample JSON File
For the examples in this guide, we'll be using a sample JSON file named largefile.json
. Here's the structure of this file:
{
"users": [
{
"name": "John Doe",
"age": 30,
"city": "New York",
"hobbies": [
"reading",
"swimming",
"cycling"
],
"address": {
"street": "123 Main St",
"zip": "10001"
}
},
{
"name": "Jane Smith",
"age": 28,
"city": "Los Angeles",
"hobbies": [
"painting",
"yoga",
"traveling"
],
"address": {
"street": "456 Oak Ave",
"zip": "90001"
}
}
]
}
Install jq
If you don't have jq installed, you can install it using:
Ubuntu/Debian:
Install jq on Ubuntu/Debian
sudo apt-get install jq
macOS (using Homebrew):
Install jq on macOS
brew install jq
Explore the Structure
To get an overview of the JSON structure:
Pretty-print JSON
jq '.' largefile.json
Sample Output:
{
"name": "John Doe",
"age": 30,
"city": "New York",
"hobbies": [
"reading",
"swimming",
"cycling"
],
"address": {
"street": "123 Main St",
"zip": "10001"
}
}
View Specific Keys
To view specific keys or values:
View specific key
jq '.name' largefile.json
Sample Output:
"John Doe"
For nested keys:
View nested key
jq '.address.street' largefile.json
Sample Output:
"123 Main St"
Search and Filter Data
To filter out specific data:
Filter data
jq '.[] \
| select(.age == 30)' largefile.json
Sample Output:
{
"name": "John Doe",
"age": 30,
"city": "New York",
"hobbies": [
"reading",
"swimming",
"cycling"
],
"address": {
"street": "123 Main St",
"zip": "10001"
}
}
Preview the First Few Entries
If the JSON is an array or a list of objects, you can view the first few entries:
Preview first few entries
jq '.[0:2]' largefile.json
Sample Output:
[
{
"name": "John Doe",
"age": 30,
"city": "New York",
"hobbies": [
"reading",
"swimming",
"cycling"
],
"address": {
"street": "123 Main St",
"zip": "10001"
}
},
{
"name": "Jane Smith",
"age": 28,
"city": "Los Angeles",
"hobbies": [
"painting",
"yoga",
"traveling"
],
"address": {
"street": "456 Oak Ave",
"zip": "90001"
}
}
]
These interactive examples demonstrate various ways to use the jq
command for JSON processing. You can adjust the parameters to explore different scenarios on your JSON data.