gron

4.4.0 • Public • Published

gron - Make JSON greppable!.

=========================================

Deps NPM version

available-for-advisory extra

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 🚀! jq.node is JavaScript and Lodash in your shell!

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 = "github@fgribreau.com";
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: 'github@fgribreau.com', 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": "github@fgribreau.com",
    "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

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!

Francois-Guillaume Ribreau (npm@fgribreau.com)

Dependencies (3)

Dev Dependencies (7)

Package Sidebar

Install

npm i gron

Weekly Downloads

3

Version

4.4.0

License

MIT

Last publish

Collaborators

  • fgribreau