• Stars
    star
    13,551
  • Rank 2,275 (Top 0.05 %)
  • Language
    Go
  • License
    MIT License
  • Created about 12 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Make JSON greppable!

gron

Build Status

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.

â–¶ gron "https://api.github.com/repos/tomnomnom/gron/commits?per_page=1" | fgrep "commit.author"
json[0].commit.author = {};
json[0].commit.author.date = "2016-07-02T10:51:21Z";
json[0].commit.author.email = "[email protected]";
json[0].commit.author.name = "Tom Hudson";

gron can work backwards too, enabling you to turn your filtered data back into JSON:

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

Disclaimer: the GitHub API has fantastic documentation, but it makes for a good example.

Installation

gron has no runtime dependencies. You can just download a binary for Linux, Mac, Windows or FreeBSD and run it. Put the binary in your $PATH (e.g. in /usr/local/bin) to make it easy to use:

â–¶ tar xzf gron-linux-amd64-0.1.5.tgz
â–¶ sudo mv gron /usr/local/bin/

If you're a Mac user you can also install gron via brew:

â–¶ brew install gron

Or if you're a Go user you can use go install:

â–¶ go install github.com/tomnomnom/gron@latest

It's recommended that you alias ungron or norg (or both!) to gron --ungron. Put something like this in your shell profile (e.g. in ~/.bashrc):

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

Or you could create a shell script in your $PATH named ungron or norg to affect all users:

gron --ungron "$@"

Usage

Get JSON from a file:

â–¶ gron testdata/two.json 
json = {};
json.contact = {};
json.contact.email = "[email protected]";
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.name = "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 something and easily see the path to it:

â–¶ gron testdata/two.json | grep twitter
json.contact.twitter = "@TomNomNom";

gron makes diffing JSON easy too:

â–¶ diff <(gron two.json) <(gron two-b.json)
3c3
< json.contact.email = "[email protected]";
---
> json.contact.email = "[email protected]";

The output of gron is valid JavaScript:

â–¶ gron testdata/two.json > tmp.js
â–¶ echo "console.log(json);" >> tmp.js
â–¶ nodejs tmp.js
{ contact: { email: '[email protected]', twitter: '@TomNomNom' },
  github: 'https://github.com/tomnomnom/',
  likes: [ 'code', 'cheese', 'meat' ],
  name: 'Tom' }

It's also possible to obtain the gron output as JSON stream via the --json switch:

â–¶ 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 also turn its output back into JSON:

â–¶ gron testdata/two.json | gron -u
{
  "contact": {
    "email": "[email protected]",
    "twitter": "@TomNomNom"
  },
  "github": "https://github.com/tomnomnom/",
  "likes": [
    "code",
    "cheese",
    "meat"
  ],
  "name": "Tom"
}

This means you use can use gron with grep and other tools to modify 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 preserve array keys, arrays are padded with null when values are missing:

â–¶ 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"
  ]
}

If you get creative you can do some pretty neat tricks with gron, and then ungron the output back into JSON.

Get Help

â–¶ gron --help
Transform JSON (from a file, URL, or stdin) into discrete assignments to make it greppable

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

Options:
  -u, --ungron     Reverse the operation (turn assignments back into JSON)
  -v, --values     Print just the values of provided assignments
  -c, --colorize   Colorize output (default on tty)
  -m, --monochrome Monochrome (don't colorize output)
  -s, --stream     Treat each line of input as a separate JSON object
  -k, --insecure   Disable certificate validation
  -j, --json       Represent gron data as JSON stream
      --no-sort    Don't sort output (faster)
      --version    Print version information

Exit Codes:
  0	OK
  1	Failed to open file
  2	Failed to read input
  3	Failed to form statements
  4	Failed to fetch URL
  5	Failed to parse statements
  6	Failed to encode JSON

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

FAQ

Wasn't this written in PHP before?

Yes it was! The original version is preserved here for posterity.

Why the change to Go?

Mostly to remove PHP as a dependency. There's a lot of people who work with JSON who don't have PHP installed.

Why shouldn't I just use jq?

jq is awesome, and a lot more powerful than gron, but with that power comes complexity. gron aims to make it easier to use the tools you already know, like grep and sed.

gron's primary purpose is to make it easy to find the path to a value in a deeply nested JSON blob when you don't already know the structure; much of jq's power is unlocked only once you know that structure.

More Repositories

1

waybackurls

Fetch all the URLs that the Wayback Machine knows about for a domain
Go
3,189
star
2

assetfinder

Find domains and subdomains related to a given domain
Go
2,818
star
3

httprobe

Take a list of domains and probe for working HTTP and HTTPS servers
Go
2,728
star
4

hacks

A collection of hacks and one-off scripts
Go
2,056
star
5

gf

A wrapper around grep, to help you grep for things
Go
1,655
star
6

meg

Fetch many paths for many hosts - without killing the hosts
Go
1,548
star
7

anew

A tool for adding new lines to files, skipping duplicates
Go
1,229
star
8

unfurl

Pull out bits of URLs provided on stdin
Go
972
star
9

qsreplace

Accept URLs on stdin, replace all query string values with a user-supplied value
Go
687
star
10

fff

The Fairly Fast Fetcher. Requests a bunch of URLs provided on stdin fairly quickly.
Go
364
star
11

dotfiles

.vimrc, .bashrc etc
Vim Script
324
star
12

rawhttp

A Go library for making HTTP requests with complete control
Go
116
star
13

linkheader

Golang HTTP Link header parser
Go
90
star
14

burl

A Broken-URL Checker
Go
78
star
15

concurl

Make concurrent requests with the curl command-line tool
Go
70
star
16

comb

Combine the lines from two files in every combination
Go
46
star
17

gahttp

Async / concurrent HTTP requests for Go
Go
45
star
18

blocksort

A tool for sorting blocks of lines
Go
34
star
19

securitytxt

A security.txt parser for Go
Go
31
star
20

vumeter

Little HTML canvas VU meter visualisation
JavaScript
28
star
21

xtermcolor

Golang package and command to convert color.Colour to the nearest xterm/bash/shell color
Go
25
star
22

phargs

A toolkit for writing CLI scripts in PHP
PHP
25
star
23

eater-cpu

Go
23
star
24

ASCIIPoint

An ASCII presentation tool in PHP
PHP
20
star
25

twarch

A Twitter Archive thing
PHP
16
star
26

symwatch

A tool to run a command when the target of a symlink changes
Go
15
star
27

cowtalks

A shell script for running lightning talks with a cow as a compere.
Shell
14
star
28

getgithubrepos

A tool to list the SSH clone URLs for all GitHub repos for a given user
Go
12
star
29

phpsecuritytxt

A security.txt parser for PHP
PHP
11
star
30

sheep

I can't draw
JavaScript
10
star
31

globwatch

Golang package to watch a glob pattern for changes.
Go
10
star
32

tomnomnom.github.io

GitHub Pages
HTML
9
star
33

tomnomnom.com

Source for tomnomnom.com
PHP
8
star
34

new-stuff-in-php-5.4

For PHP Leeds
PHP
8
star
35

phpwol

Wake On LAN for PHP
PHP
8
star
36

git-talk

Slides for teaching PlatOps people about git
6
star
37

go-learning

Bits and pieces of Go while I'm learning
Go
6
star
38

Pwas

A webserver written in PHP
PHP
5
star
39

PHP-Evolution-Sim

Basic evolution simulated in PHP
PHP
4
star
40

gotemplate

Template for my Go projects
Shell
4
star
41

leedshack2018

A stack-based VM and assembler, built in 2 days at Leeds Hack 2018
Go
4
star
42

Writing-Testable-PHP

Talk from The Digital Barn
PHP
4
star
43

rplex

A simple general purpose lexer library for Go
Go
4
star
44

fclock

Toy rotating bar clock canvas thing
JavaScript
3
star
45

graphite-client

Really simple plain-text graphite client
JavaScript
3
star
46

api.tomnomnom.com

API shenanigans
PHP
3
star
47

finishingtouchautos.co.uk

Source for finishingtouchautos.co.uk
CSS
3
star
48

tomhudson.co.uk

Source for tomhudson.co.uk
PHP
3
star
49

ansible-play

Playing with ansible
Smarty
3
star
50

build-a-vm-talk

Code and slides from my Hey!Stac talk at the Belgrave in Leeds on the 28th of October 2014.
PHP
2
star
51

crtmas

Merry CRTmas
JavaScript
2
star
52

myfirstwebsite

Test repo plz ignore
PHP
2
star
53

tacho

HTML Canvas Tachometer
JavaScript
2
star
54

bouncy.tomnomnom.com

Canvas Bouncy Ball
JavaScript
2
star
55

flatclass

Flatten deep inheritance trees in PHP to aid debugging
PHP
2
star
56

sbt.org.uk

Source for sbt.org.uk
PHP
2
star
57

branchdemo

CPU Branch Predictor Demo
Go
2
star
58

All-About-SPL

All About SPL talk for LeedsPHP
PHP
2
star
59

readable-code

Slides for Readable Code talk
PHP
1
star
60

victoria-hudson.co.uk

victoria-hudson.co.uk source
PHP
1
star
61

n-things-about-mongo

Slides from LeedsPHP talk on 2013-08-19
1
star
62

unit-testing

Unit testing examples
PHP
1
star
63

node-in-production-talk

Node In Production talk slides etc for NodeUpNorth
JavaScript
1
star
64

fixaholic.uk

Source for fixaholic.uk
PHP
1
star
65

rainbow-waves

Canvas Rainbow Waves
JavaScript
1
star
66

Sbt

SBT library of... stuff.
PHP
1
star
67

numbers.tomnomnom.com

Source for numbers.tomnomnom.com
Go
1
star