Coder Social home page Coder Social logo

gjq's Introduction

gjq

gjq is a simple jq-like program with performance rather than completeness as the goal.

It only implements what I've needed, which at the moment is extracting fields from a series of messages. For example:

$ echo '[ {"x":1}, {"x":2}, {"x":3} ]' | gjq '.[].x'
1
2
3

At the moment gjq is about 5x faster than jq for the sort of input data and filter I'm interested in using it on:

$ time gjq '.X[].Y' <100k_json_records.json >/dev/null
real    0m12.647s
user    0m12.072s
sys     0m0.544s
$ time jq '.X[].Y' <100k_json_records.json >/dev/null
real    1m3.752s
user    1m2.780s
sys     0m0.780s
$ ls -l 100k_json_records.json
-rw-rw-r-- 1 ndade ndade 2719975876 Jun  2 07:52 100k_json_records.json

The test input is 100,000 lines, each line is a single compacted JSON object. The average size of an object in this file is 27kB, and I'm extracting a small (20 byte) field from a handful of list elements in each object. The output contains 171k elements.

Another performance comparison, 8x in this case, which consists of outputting everything in compacted form (compacted is the default for gjq since it's better for performance; use gjq -pretty for formatted output):

$ time gjq '.' <100k_json_records.json >/dev/null
real    0m14.569s
user    0m13.936s
sys     0m0.592s
$ time jq -c '.' <100k_json_records.json >/dev/null
real    2m4.632s
user    2m3.708s
sys     0m0.848s

Note passing jq the -c flag helps it. Without -c the jq time is 2m29s.

Multiple fields can be extracted in one pass. Separate them with ,

gjq .X,.Y

gjq supports comments in JSON. Comments start with a # symbol and extend to the end of the line. Comments aren't part of the JSON standard, of course, but they are darn useful never-the-less.

gjq allows the numeric values 'NaN', 'Inf', '+Inf" and '-Inf'. These are not part of the JSON standard, but some JSON contains them anyway due to how the encoder was written.

gjq's People

Contributors

nsd20463 avatar toransahu avatar

Stargazers

 avatar  avatar Jitendra Harlalka avatar Gurpreet Singh avatar David Luu avatar

Watchers

James Cloos avatar  avatar

Forkers

toransahu

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.