Page
Library
Module
Module type
Parameter
Class
Class type
Source
query-json is a faster, simpler and more portable implementation of the jq language in OCaml distributed as a binary, but also distributed as a JavaScript package via js_of_ocaml.
query-json allows you to write small programs to operate on top of json files with a concise syntax.
It was created with mostly two reasons in mind, learning and having fun
menhir, sedlex and friends and try to make great error messages.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...)query-json implements most of jq 1.8's functionality with some intentional improvements:
Better naming - snake_case instead of alllowercase:
to_number / to_string (instead of tonumber / tostring)starts_with / ends_with (instead of startswith / endswith)is_nan (instead of isnan)Extra conveniences:
filter(expr) - alias for map(select(expr))flat_map(expr) - map and flatten in one operationfind(expr) - find first matching elementsome(expr) - check if at least one element matchesunique - accepts both unique and uniq (jq only has uniq)., .foo, .[], .[0], .[1:3], etc.)+, -, *, /, %, ==, !=, <, >, <=, >=, and, or, not)if-then-else)|), comma (,), alternative (//)map, select, length, keys, has, in, add, reverse, etc.)sort, sort_by, unique, unique_by, group_by, flatten, min, max, etc.)split, join, startswith, endswith, contains, explode, implode)type, to_number, to_string)abs, floor, sqrt, ceil, round, sin, cos, tan, log, exp, etc.)to_entries, from_entries, with_entries)path, paths, getpath, setpath, del)while, until, recurse, walk, limit, try-catch, reduce)test, match, scan, capture, sub, gsub)User-defined functions (def), modules (import, include), format strings (@text, @csv, @base64), and running tests (--run-tests).
For a complete reference, see the jq manual.
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 | bashnpm install --global @davesnx/query-jsonI recommend to write the query in single-quotes inside the terminal, since writting JSON requires double-quotes for accessing properties.
query-json '.' pokemons.jsoncat pokemons.json | query-json '.'
query-json '.' <<< '{ "bulvasur": { "id": 1, "power": 20 } }'query-json '.' '{ "bulvasur": { "id": 1, "power": 20 } }'query-json '.' pokemons.json --no-colorsquery-json -r '.name' pokemon.json
# Output: Pikachu
# Instead of: "Pikachu"Check out docs/examples.md for a walkthrough of common use cases.
query-json consistently outperforms jq 1.8.1 across most file sizes and operations, with performance improvements ranging from 1.5x to 4.5x faster depending on the file size and operation:
def) - intentional tradeoff for better performanceimport, include) - not implemented@text, @csv, @base64, etc.) - not implemented--run-testsFor detailed benchmarks and methodology, see benchmarks/README.md.
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 on x or email dsnxmoreno at gmail dot com.
I usually hang out at discord.gg/reasonml feel free to DM.
Requirements: opam
git clone https://github.com/davesnx/query-json
cd query-json
make init # creates opam switch, installs ocaml deps and npm deps
make dev-core # compiles query-json "core" only
make test # runs unit tests and snapshots tests
dune exec query-json # Run binaryRunning the playground
# In different terminals
make dev # compiles all packages "query-json" "query-json-js" and "query-json-playground", and runs the bundler
make web-dev # Runs bundler and the web server