• Stars
    star
    574
  • Rank 77,748 (Top 2 %)
  • Language Reason
  • License
    BSD 3-Clause "New...
  • Created about 4 years ago
  • Updated over 1 year ago

Reviews

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

Repository Details

Faster, simpler and more portable implementation of `jq` in Reason



query-json logo query-json logo


Coverage Status Netlify Badge


query-json is a faster, simpler and more portable implementation of the jq language in Reason distributed as a dependency-free binary thanks to the OCaml compiler, and distributed to the web with js_of_ocaml.

query-json allows you to write small programs to operate on top of json files with a concise syntax.

asciicast

Purpose

It was created with mostly two reasons, learning and having fun.

  • Learn how to write a Programming Language with the OCaml stack using menhir and sedlex with great error messages.
  • Create a CLI tool in Reason Native and being able to distribute it as a binary (for performance) and as a JavaScript library (for portability).

It brings

  • Great Performance: Fast, small footprint and minimum run-time. Check Performance section for a longer explanation.
  • Delightful errors:
    • Better errors when json types and operation don't match:
      $ query-json '.esy.release.wat' esy.json
      Error:  Trying to ".wat" on an object, that don't have the field "wat":
      { "bin": ... }
    • debug prints the tokens and the AST.
    • verbose flag, prints each operation in each state and it's intermediate states. (Work in progress...)
  • Improved API: made small adjustments to the buildin operations. Some examples are:
    • All methods are snake_case instead of alltoghetercase
    • Added filter(p) as an alias for map(select(p))
    • Supports comments in JSONs
  • Small: Lexer, Parser and Compiler are just 300 LOC and most of the commands that I use on my day to day are implemented in only 140 LOC.

Installation

Using a bash script

Check the content of scripts/install.sh before running anything in your local. Friends don't let friends curl | bash.

curl -sfL https://raw.githubusercontent.com/davesnx/query-json/master/scripts/install.sh | bash

Using npm/yarn

npm install --global @davesnx/query-json
# or
yarn global add @davesnx/query-json

Download zip files from GitHub

Usage

I recommend to write the query in single-quotes inside the terminal, since writting JSON requires double-quotes for accessing properties.

NOTE: I have aliased query-json to be "q" for short, you can set it in your dotfiles. alias q="query-json".

query a json file

q '.' pokemons.json

query from stdin

cat pokemons.json | q '.'

query a json inlined

q --kind=inline '.' '{ "bulvasur": { "id": 1, "power": 20 } }'

query without colors

q '.' pokemons.json --no-colors

Performance

This report is not an exhaustive performance report of both tools, it's a overview for the percieved performance of the user. Here I don't profile each tool and try to see what are the bootlenecks, since I assume that both tools have the penalty of parsing a JSON file. Simply run a bash script and analyze the results.

Aside from that, query-json doesn't have feature parity with jq which is ok at this point, but jq contains a ton of functionality that query-json misses. Adding the missing operations on query-json won't affect the performance of it, that could not be true for features like "modules" or "tests", which they will not be implemented in query-json.

The report shows that query-json is between 2x and 5x faster than jq in all operations tested and same speed (~1.1x) with huge files (> 100M).

Currently supported feature set:

Badge Meaning
Implemented
⚠️ Not implemented yet
🔴 Won't implement
Based on jq 1.6

CLI: Invoking jq

  • --version
  • --kind. This is different than jq ✅
    • --kind=file and the 2nd argument can be a json file
    • --kind=inline and the 2nd argument can be a json as a string
  • --no-color. This disables colors ✅
  • ...rest ⚠️

Basic filters

  • Identity: .
  • Object Identifier-Index: .foo, .foo.bar
  • Optional Object Identifier-Index: .foo?
  • Generic Object Index: .[<string>]
  • Array Index: .[2]
  • Pipe: |
  • Array/String Slice: .[10:15] ⚠️
  • Array/Object Value Iterator: .[]
  • Comma: ,
  • Parenthesis: () ✅️

Types and Values ⚠️

Builtin operators and functions

  • Addition: +
  • Subtraction: -
  • Multiplication, division, modulo: *, /, and %
  • length
  • keys
  • map
  • select
  • has(key) ⚠️
  • in ⚠️
  • path(path_expression) ⚠️
  • to_entries, from_entries, with_entries ⚠️
  • any, any(condition), any(generator; condition) ⚠️
  • all, all(condition), all(generator; condition) ⚠️
  • flatten
  • range(upto), range(from;upto) range(from;upto;by) ⚠️
  • floor, sqrt ⚠️
  • tonumber, tostring ⚠️
  • type ⚠️
  • infinite, nan, isinfinite, isnan, isfinite, isnormal ⚠️
  • sort, sort_by(path_expression)
  • group_by(path_expression) ⚠️
  • min, max, min_by(path_exp), max_by(path_exp) ⚠️
  • unique, unique_by(path_exp) ⚠️
  • reverse ⚠️
  • contains(element) ⚠️
  • index(s), rindex(s) ⚠️
  • startswith(str), endswith(str) ⚠️
  • explode, implode ⚠️
  • split(str), join(str) ⚠️
  • while(cond; update), until(cond; next) ⚠️
  • recurse(f), recurse, recurse(f; condition), recurse_down ⚠️
  • walk(f) ⚠️
  • transpose(f) ⚠️
  • Format strings and escaping: @text, @csv, etc.. 🔴

Conditionals and Comparisons

  • ==, !=
  • if-then-else ⚠️
  • >, >=, <=, <
  • and, or, not ⚠️
  • break 🔴

Regular expressions (PCRE) ⚠️

Advanced features ⚠️

Assignment ⚠️

Modules ⚠️

Contributing

Contributions are what make the open source community such an amazing place to be, learn, inspire, and create. Any contributions you make are greatly appreciated. If you have any questions just contact me @twitter or email dsnxmoreno at gmail dot com.

Support

I usually hang out at discord.gg/reasonml or reasonml.chat so feel free to ask anything there.

Setup

Requirements: esy

git clone https://github.com/davesnx/query-json
cd query-json
esy # installs
esy test # runs unit tests with [rely](https://reason-native.com/docs/rely), live under test/.
esy bin # Run binary

Acknowledgements

Thanks to @EduardoRFS. Thanks to all the authors of dependencies that this project relies on: menhir, sedlex, yojson. Thanks to the OCaml and Reason Native team.

More Repositories

1

styled-ppx

Type-safe styled components for ReScript, Melange and native with type-safe CSS
Raku
401
star
2

learn-ramda

🐏 Learn ramda, the interactive way
JavaScript
89
star
3

taco

Layout primitives written with ReasonReact and styled-ppx
Reason
56
star
4

babel-plugin-transform-react-qa-classes

Add component's name in `data-qa` attributes to React Components
JavaScript
41
star
5

html_of_jsx

Render HTML with JSX
OCaml
31
star
6

ocaml-box

Render boxes in the terminal with OCaml or Reason
Reason
27
star
7

reason-console-formatter

Transforms ReasonML types more readable format when they are logged to the console
JavaScript
27
star
8

dinosaur

🦖 Chrome "No internet connection" Dinosaur game, build with Revery.
OCaml
20
star
9

awesome-ppx-deriving

A collection of ppx-deriving plugins
11
star
10

FullStackFest-2016-Notes

Notes and some transcriptions of Full Stack Fest 2016
10
star
11

setup

dotfiles, cli-tools, editor settings, apps, browser extensions
Shell
9
star
12

try-styled-ppx

Demo of styled-ppx
Reason
9
star
13

gulpES6ify

gulp boilerplate for ES6
JavaScript
8
star
14

postureo.io

Example of a Promise-based Scrapper
JavaScript
7
star
15

yolomanifesto

Yolo Manifesto Website
JavaScript
7
star
16

rxduce

Intent to implement redux-like on top of RxJS
JavaScript
7
star
17

melange-latest-esy-template

melange-re/melange-esy-template with latest melange/mel/meldep/dune
Reason
6
star
18

ReactiveConf-2016-Notes

Notes and some transcriptions of ReactiveConf 2016
6
star
19

re-spring-css

CSS-keyframes animations in Reason
JavaScript
6
star
20

dune-fmt

An opinionated dune file formatter CLI
Reason
6
star
21

sancho.dev

Personal website
TypeScript
5
star
22

docker-alpine-esy

Dockerfile
3
star
23

functional-workshops

A serie of workshops runned at Typeform about FP
JavaScript
3
star
24

reason-storybook-example

Example Storybook with ReasonML
JavaScript
3
star
25

awesome-ppx

2
star
26

shelm

Basic shell app for learning purposes
Elm
2
star
27

reason-ci-gh-actions

OCaml
1
star
28

try-react-rules-of-hooks-ppx

Reason
1
star
29

realrockstars

JavaScript
1
star
30

reason-styled-functor

Reason
1
star
31

CodeSandbox-OCaml-4.14

Created with CodeSandbox
1
star
32

Yo-API-client

Yo API Client in NodeJS
JavaScript
1
star
33

rescript-bindings-handbook

HTML
1
star
34

learn-reason-workshop

Reason
1
star
35

vscode-fosk-theme

TypeScript
1
star
36

davesnx

1
star
37

OCaml-4.14

Created with CodeSandbox
1
star
38

dune_ppx_issue_flags

Shell
1
star