gron - Make JSON greppable!.
Make JSON greppable!
gron transforms JSON into discrete assignments to make it easier to grep for what you want and see the absolute 'path' to it. It eases the exploration of APIs that return large blobs of JSON but have terrible documentation.
▶ curl -s https://jsonplaceholder.typicode.com/users | gron | fgrep "company.name"
json[0].company.name = "Romaguera-Crona";
json[1].company.name = "Deckow-Crist";
json[2].company.name = "Romaguera-Jacobson";
json[3].company.name = "Robel-Corkery";
json[4].company.name = "Keebler LLC";
json[5].company.name = "Considine-Lockman";
json[6].company.name = "Johns Group";
json[7].company.name = "Abernathy Group";
json[8].company.name = "Yost and Sons";
json[9].company.name = "Hoeger LLC";
gron can work backwards too, enabling you to turn your filtered data back into JSON:
▶ curl -s https://jsonplaceholder.typicode.com/users | gron | fgrep "company.name" | ungron
[
{
"company": {
"name": "Romaguera-Crona"
}
},
{
"company": {
"name": "Deckow-Crist"
}
},
...
...
You like this?
If you like Gron, you will find jq.node awesome
Installation
Install with npm.
npm install -g gron
Usage
Get JSON from a file:
▶ cat testdata/two.json | gron
json = {};
json.name = "FGRibreau";
json.github = "https://github.com/fgribreau/";
json.likes = [];
json.likes[0] = "code";
json.likes[1] = "cheese";
json.likes[2] = "meat";
json.contact = {};
json.contact.email = "[email protected]";
json.contact.twitter = "@FGRibreau";
From a URL:
▶ curl -s http://headers.jsontest.com/ | gron
json = {};
json["X-Cloud-Trace-Context"] = "e76953d4e0a7a4c00a60d3d8329d0236/11387270255695883695";
json.Host = "headers.jsontest.com";
json["User-Agent"] = "curl/7.43.0";
json.Accept = "*/*";
Grep for something and easily see the path to it:
▶ cat testdata/two.json | gron | grep twitter
json.contact.twitter = "@FGRibreau";
gron makes diffing JSON easy too:
▶ diff <(cat testdata/two.json | gron) <(cat testdata/two-b.json | gron)
10c10
< json.contact.twitter = "@FGRibreau";
---
> json.contact.twitter = "@fgribreau";
The output of gron is valid JavaScript:
▶ cat testdata/two.json | gron > tmp.js
▶ echo "console.log(json);" >> tmp.js
▶ node tmp.js
{ name: 'FGRibreau',
github: 'https://github.com/fgribreau/',
likes: [ 'code', 'cheese', 'meat' ],
contact: { email: '[email protected]', twitter: '@FGRibreau' } }
ungronning
gron can also turn its output back into JSON:
▶ cat testdata/two.json | gron | ungron
{
"name": "FGRibreau",
"github": "https://github.com/fgribreau/",
"likes": [
"code",
"cheese",
"meat"
],
"contact": {
"email": "[email protected]",
"twitter": "@FGRibreau"
}
}
This means you use can use gron with grep and other tools to modify JSON:
▶ cat testdata/two.json | gron | grep likes | ungron
{
"likes": [
"code",
"cheese",
"meat"
]
}
To preserve array keys, arrays are padded with null when values are missing:
▶ cat testdata/two.json | gron | grep likes | grep -v cheese
json.likes = [];
json.likes[0] = "code";
json.likes[2] = "meat";
▶ cat testdata/two.json | gron | grep likes | grep -v cheese | ungron
{
"likes": [
"code",
null,
"meat"
]
}
Changelog
❤️ Shameless plug
- Open-Source self-hostable or fully-managed webhook as a service
- Looking for a managed Keycloak IAM ?
- Charts, simple as a URL. No more server-side rendering pain, 1 url = 1 chart
Todo
This whole project (up to v2.0.1, from idea to this README) was done in 1 hour, so there is some missing features in this implementation (if you can call 3 line of codes an implementation).
- stream support (large file support)
Credits
This module is entirely inspired by tomnomnom/gron but instead of reinventing the wheel it relies on nodejs+flat.
You want to support my work?
I maintain this project in my free time, if it helped you, well, I would be grateful to buy a beer thanks to your paypal or Bitcoins, donation!