Introduction
<span><span>gron</span></span> is a unique command-line tool that converts <span><span>JSON</span></span> data into discrete, easily <span><span>grep</span></span>-processable assignment statement format. Its name comes from “<span><span>grepable on</span></span>” or “<span><span>grepable JSON</span></span>“, primarily addressing the challenge of handling complex <span><span>JSON</span></span> data in the command line.
Core Value
<span><span>gron</span></span> is centered around flattening <span><span>JSON</span></span> data into a format similar to <span><span>json.path.to.key = value</span></span>;.
-
Simplified
<span><span>JSON</span></span>processing: Flattens nested<span><span>JSON</span></span>structures into a searchable format. -
Enhanced
<span><span>grep</span></span>capabilities: Enables standard text tools to efficiently process<span><span>JSON</span></span>data. -
Reversible conversion: Allows the processed data to be restored to the original
<span><span>JSON</span></span>
Installation
<span><span>Ubuntu/Debian</span></span>
sudo apt install gron
<span><span>CentOS/RHEL</span></span>
sudo yum install epel-release
sudo yum install gron
<span><span>macOS</span></span>
brew install gron
- Install from source (
<span><span>Go</span></span>)
go install github.com/tomnomnom/gron@latest
Common Options
-
<span><span>-c, --color</span></span>: Force color output (even in non-terminal environments). -
<span><span>-i, --indent</span></span>: Specify the number of spaces for indentation (default is 2). -
<span><span>-n, --no-sort</span></span>: Do not sort the output results. -
<span><span>-u, --ungron</span></span>: Convert<span><span>gron</span></span>format back to<span><span>JSON</span></span>. -
<span><span>--json</span></span>: Equivalent to<span><span>--ungron</span></span>, but more semantically appropriate. -
<span><span>-v, --values</span></span>: Output only the values part (excluding paths). -
<span><span>-s, --stream</span></span>: Treat each line of input as a separate<span><span>JSON</span></span>object for processing.
Example Usage
<span><span>data.json</span></span> file example:
{
"name":"Alice",
"age":30,
"pets":[
{"name":"Rex","type":"dog"},
{"name":"Whiskers","type":"cat"}
],
"address":{
"city":"New York",
"zip":"10001"
}
}
Convert JSON to gron format
gron data.json
Output:
json = {};
json.name = "Alice";
json.age = 30;
json.pets = [];
json.pets[0] = {};
json.pets[0].name = "Rex";
json.pets[0].type = "dog";
json.pets[1] = {};
json.pets[1].name = "Whiskers";
json.pets[1].type = "cat";
json.address = {};
json.address.city = "New York";
json.address.zip = "10001";
Search for a specific value
gron data.json | grep "zip"
Output:
json.address.zip = "10001";
Restore to JSON format (–ungron)
gron data.json | grep "pets" | gron --ungron
Output:
{
"pets": [
{
"name": "Rex",
"type": "dog"
},
{
"name": "Whiskers",
"type": "cat"
}
]
}
Use custom variable names (-s)
gron -s data data.json
Output:
data = {};
data.name = "Alice";
Stream processing of large files (–stream)
curl -s https://api.example.com/large-data | gron --stream
Specify output format (-j, –json)
gron data.json -j | grep "name"
Output:
$.name = "Alice";
$.pets[0].name = "Rex";
$.pets[1].name = "Whiskers";
Combine awk to process data
gron data.json | awk '/pets/ && /type/ {print $3}'
Output:
"dog"
"cat"
Modify and restore data
gron data.json | sed 's/"New York"/"Boston"/' | gron --ungron
Process multiple files
gron file1.json file2.json | grep "error"
Use jq style paths
gron -j data.json | grep 'pets.*name'
Output:
$.pets[0].name = "Rex";
$.pets[1].name = "Whiskers";
Debug API responses
curl -s https://api.github.com/users/octocat | gron | grep "company"
Process complex configuration files
gron config.json | grep "database.password"
Search nested values
gron data.json | grep "pets.*cat"
Extract all key names
gron data.json | awk -F '.' '{print $2}' | sort | uniq
Process compressed data
zcat large.json.gz | gron --stream | grep "error"
Color highlight output
gron data.json | grep --color=auto "name"
Please open in the WeChat client