Gron – Make JSON Greppable

Last modified on November 08, 2020

Build Status

Maintain JSON greppable!

gron transforms JSON into discrete assignments to hold out it easier to grep for what you scheme terminate to admire and glimpse absolutely the 'course' to it.
It eases the exploration of APIs that return immense blobs of JSON however admire gradual documentation.

gron "https://api.github.com/repos/tomnomnom/gron/commits?per_page=1" | fgrep "commit.creator"
json[0].commit.creator={};
json[0].commit.creator.date="2016-07-02T10: 51: 21Z";
json[0].commit.creator.e-mail="mail@tomnomnom.com";
json[0].commit.creator.identify="Tom Hudson";

gron can work backwards too, enabling you to reveal your filtered information once more into JSON:

▶ gron "https://api.github.com/repos/tomnomnom/gron/commits?per_page=1" | fgrep "commit.creator" | gron --ungron
[
  {
    "commit": {
      "author": {
        "date": "2016-07-02T10:51:21Z",
        "email": "mail@tomnomnom.com",
        "name": "Tom Hudson"
      }
    }
  }
]

Disclaimer: the GitHub API has inconceivable documentation, nevertheless it makes for a factual instance.

Installation

gron has no runtime dependencies. That you merely might maybe additionally precise obtain a binary for Linux, Mac, Home home windows or FreeBSD and escape it.
Put the binary in your $PATH (e.g. in /usr/native/bin) to hold out it simple to make use of:

▶ tar xzf gron-linux-amd64-0.1.5.tgz
▶ sudo mv gron /usr/native/bin/

Must you're a Mac specific particular person you can also furthermore arrange gron by strategy of brew:

▶ brew arrange gron

Or for these that're a Ride specific particular person you can also use hasten rep (for these that're the utilization of Ride 1.7 or newer):

▶ hasten rep -u github.com/tomnomnom/gron

It's actually useful that you just simply alias ungron or norg (or each!) to gron --ungron. Put one thing esteem this in your shell profile (e.g. in ~/.bashrc):

alias norg="gron --ungron"
alias ungron="gron --ungron"

Or you'll be able to obtain a shell script in your $PATH named ungron or norg to admire an ticket on all customers:

gron --ungron "$@"

Utilization

Obtain JSON from a file:

▶ gron testdata/two.json 
json={};
json.contact={};
json.contact.e-mail="mail@tomnomnom.com";
json.contact.twitter="@TomNomNom";
json.github="https://github.com/tomnomnom/";
json.likes=[];
json.likes[0]="code";
json.likes[1]="cheese";
json.likes[2]="meat";
json.identify="Tom";

From a URL:

▶ gron http://headers.jsontest.com/
json={};
json.Host="headers.jsontest.com";
json["User-Agent"]="gron/0.1";
json["X-Cloud-Trace-Context"]="6917a823919477919dbc1523584ba25d/11970839830843610056";

Or from stdin:

▶ curl -s http://headers.jsontest.com/ | gron
json={};
json.Accept="*/*";
json.Host="headers.jsontest.com";
json["User-Agent"]="curl/7.43.0";
json["X-Cloud-Trace-Context"]="c70f7bf26661c67d0b9f2cde6f295319/13941186890243645147";

Grep for one thing and effortlessly glimpse the hasten to it:

▶ gron testdata/two.json | grep twitter
json.contact.twitter="@TomNomNom";

gron makes diffing JSON simple too:

▶ diff  json.contact.e mail="contact@tomnomnom.com";

The output of gron is legitimate JavaScript:

▶ gron testdata/two.json> tmp.js
▶ echo "console.log(json);">> tmp.js
▶ nodejs tmp.js
{ contact: { e-mail: 'mail@tomnomnom.com', twitter: '@TomNomNom' },
  github: 'https://github.com/tomnomnom/',
  likes: [ 'code', 'cheese', 'meat' ],
  identify: 'Tom' }

It's furthermore possibly to assemble the gron output as JSON flow into by strategy of
the --json swap:

▶ curl -s http://headers.jsontest.com/ | gron --json
[[],{}]
[["Accept"],"*/*"]
[["Host"],"headers.jsontest.com"]
[["User-Agent"],"curl/7.43.0"]
[["X-Cloud-Trace-Context"],"c70f7bf26661c67d0b9f2cde6f295319/13941186890243645147"]

ungronning

gron can furthermore flip its output once more into JSON:

▶ gron testdata/two.json | gron -u
{
  "contact": {
    "e-mail": "mail@tomnomnom.com",
    "twitter": "@TomNomNom"
  },
  "github": "https://github.com/tomnomnom/",
  "likes": [
    "code",
    "cheese",
    "meat"
  ],
  "identify": "Tom"
}

This suggests you exhaust can use gron with grep and numerous devices to change JSON:

▶ gron testdata/two.json | grep likes | gron --ungron
{
  "likes": [
    "code",
    "cheese",
    "meat"
  ]
}

or

▶ gron --json testdata/two.json | grep likes | gron  --json --ungron
{
  "likes": [
    "code",
    "cheese",
    "meat"
  ]
}

To retain array keys, arrays are padded with null when values are lacking:

▶ gron testdata/two.json | grep likes | grep -v cheese
json.likes=[];
json.likes[0]="code";
json.likes[2]="meat";
▶ gron testdata/two.json | grep likes | grep -v cheese | gron --ungron
{
  "likes": [
    "code",
    null,
    "meat"
  ]
}

Must you rep inventive you can also invent some quite neat strategies with gron, and
then ungron the output once more into JSON.

Obtain Wait on

▶ gron --aid
Remodel JSON (from a file, URL, or stdin) into discrete assignments to hold out it greppable

Utilization:
  gron [OPTIONS] [FILE|URL|-]

Ideas:
  -u, --ungron     Reverse the operation (flip assignments once more into JSON)
  -c, --colorize   Colorize output (default on tty)
  -m, --monochrome Monochrome (invent no longer colorize output)
  -s, --circulate     Treat every line of enter as a separate JSON object
  -k, --afraid   Disable certificates validation
  -j, --json       Disclose gron information as JSON flow into
      --no-kind    Don't type output (sooner)
      --version    Print model information

Exit Codes:
  0	OK
  1	Failed to originate file
  2	Failed to be taught enter
  3	Failed to type statements
  4	Failed to catch URL
  5	Failed to parse statements
  6	Failed to encode JSON

Examples:
  gron /tmp/apiresponse.json
  gron http://jsonplaceholder.typicode.com/customers/1 
  curl -s http://jsonplaceholder.typicode.com/customers/1 | gron
  gron http://jsonplaceholder.typicode.com/customers/1 | grep firm | gron --ungron

FAQ

Wasn't this written in PHP earlier than?

Yes it turned as soon as! The typical model is preserved right here for posterity.

Why the artificial to Ride?

Largely to bewitch away PHP as a dependency. There is a substantial amount of of us who work with JSON who invent no longer admire PHP put in.

Why mustn't I precise use jq?

jq is superior, and so much more principal than gron, however with that vitality comes
complexity. gron aims to hold out it easier to make use of the devices you understand, esteem grep and sed.

gron's foremost trustworthy is to hold out it simple to derive the hasten to a label in a deeply nested JSON blob
if you happen to invent no longer already know the construction; unparalleled of jq's vitality is unlocked supreme when that construction.

Read More

Similar Products:

    None Found

Recent Content

link to HTTPWTF

HTTPWTF

HTTP is fundamental to modern development, from frontend to backend to mobile. But like any widespread mature standard, it's got some funky skeletons in the closet. Some of these skeletons are...