Coder Social home page Coder Social logo

gron's Introduction


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 = "[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"
  ]
}

Todo

This whole projet (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):

  • add color highlighting

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 ([email protected])

gron's People

Contributors

fgribreau avatar csabapalfi avatar moeriki avatar

Watchers

James Cloos avatar Ingwar Wirjawan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.