• Stars
    star
    1,471
  • Rank 31,964 (Top 0.7 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created about 6 years ago
  • Updated 4 months ago

Reviews

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

Repository Details

A JSON Query Language CLI tool

jql


GitHub Workflow Status Crates.io Docs.rs Docs.rs

jql is a JSON Query Language tool built with Rust 🦀.

Pronounce it as jackal 🐺.

📜 Philosophy

  • ⚡Be fast
  • 🪶 Stay lightweight
  • 🎮 Keep its features as simple as possible
  • 🧠 Avoid redundancy
  • 💡 Provide meaningful error messages
  • 🍰 Eat JSON as input, process, output JSON back

🚀 Installation

Alpine Linux

The package is maintained by @jirutka.

apk add jql

Archlinux

The AUR package is maintained by @barklan.

yay -S jql

Cargo

cargo install jql

Fedora

dnf install jql

FreeBSD

pkg install jql

Homebrew

brew install jql

Nix

nix-env -i jql

openSUSE

zypper install jql

Manual installation from GitHub

Compiled binary versions are automatically uploaded to GitHub when a new release is made. You can install jql manually by downloading a release.

🛠️ Usage

To make a selection from a JSON input, jql expects a query as a sequence of tokens.

To be fully compliant with the JSON format, jql always expect key selectors to be double-quoted, see The JavaScript Object Notation (JSON) Data Interchange Format.

{
  ".valid": 1337,
  "": "yeah!",
  "\"": "yup, valid too!"
}

Consequently, to be shell compliant, a query must be either enclosed by single quotation marks or every inner double quotation mark must be escaped.

Separators

Group separator

Group separators build up an array from sub-queries.

JSON input

{ "a": 1, "b": 2, "c": 3 }

Query

'"a","b","c"'

JSON output

[1, 2, 3]

Selectors

Arrays

Array index selector

Indexes can be used in arbitrary order.

JSON input

[1, 2, 3]

Query

'[2,1]'

JSON output

[3, 2]
Array range selector

Range can be in natural order [0:2], reversed [2:0], without lower [:2] or upper bound [0:].

JSON input

[1, 2, 3]

Query

'[2:1]'

JSON output

[3, 2]
Lens selector

Lens can be a combination of one or more selectors with or an optional value, a value being any of boolean | null | number | string.

JSON input

[
  { "a": 1, "b": { "d": 2 } },
  { "a": 2, "b": "some" },
  { "a": 2, "b": { "d": null } },
  { "a": 2, "b": true },
  { "c": 3, "b": 4 }
]

Query

'|={"b""d"=2, "c"}'

JSON output

[
  { "a": 1, "b": { "d": 2 } },
  { "c": 3, "b": 4 }
]

Objects

Key selector

Any valid JSON key can be used.

JSON input

{ "a": 1, "b": 2, "c": 3 }

Query

'"c"'

JSON output

3
Multi key selector

Keys can be used in arbitrary order.

JSON input

{ "a": 1, "b": 2, "c": 3 }

Query

'{"c","a"}'

JSON output

{ "c": 3, "a": 1 }
Object index selector

Indexes can be used in arbitrary order.

JSON input

{ "a": 1, "b": 2, "c": 3 }

Query

'{2,0}'

JSON output

{ "c": 3, "a": 1 }
Object range selector

Range can be in natural order {0:2}, reversed {2:0}, without lower {:2} or upper bound {0:}.

JSON input

{ "a": 1, "b": 2, "c": 3 }

Query

'{2:1}'

JSON output

{ "c": 3, "b": 2 }

Operators

Flatten operator

Flattens arrays and objects.

JSON input

[[[[[[[[[[[[[[{ "a": 1 }]]]]]]]]]]]]], [[[[[{ "b": 2 }]]]], { "c": 3 }], null]

Query

'..'

JSON output

[{ "a": 1 }, { "b": 2 }, { "c": 3 }, null]

JSON input

{ "a": { "c": false }, "b": { "d": { "e": { "f": 1, "g": { "h": 2 } } } } }

Query

'..'

JSON output

{
  "a.c": false,
  "b.d.e.f": 1,
  "b.d.e.g.h": 2
}
Pipe in operator

Applies the next tokens in parallel on each element of an array.

JSON input

{ "a": [{ "b": { "c": 1 } }, { "b": { "c": 2 } }] }

Query

'"a"|>"b""c"'

JSON output

[1, 2]
Pipe out operator

Stops the parallelization initiated by the pipe in operator.

JSON input

{ "a": [{ "b": { "c": 1 } }, { "b": { "c": 2 } }] }

Query

'"a"|>"b""c"<|[1]'

JSON output

2
Truncate operator

Maps the output into simple JSON primitives boolean | null | number | string | [] | {}.

JSON input

{ "a": [1, 2, 3] }

Query

'"a"!'

JSON output

[]

💻 Shell integration

How to save the output

jql '"a"' input.json > output.json

How to read from stdin

cat test.json | jql '"a"'

Available flags

Inline the JSON output

By default, the output is pretty printed in a more human-readable way, this can be disabled.

-i, --inline

Read the query from file

The command will read the provided query from a file instead of the stdin.

-q, --query <FILE>

Write to stdout without JSON double-quotes

This can be useful to drop the double-quotes surrounding a string primitive.

-r, --raw-string

Read a stream of JSON data line by line

This flag is only about reading processing any JSON output streamed line by line (e.g. Docker logs with the --follow flag). This is not an option to read an incomplete streamed content (e.g. a very large input).

-s, --stream

Validate the JSON data

The command will return a matching exit code based on the validity of the JSON content or file provided.

-v, --validate

Print help

-h, --help

Print version

-V, --version

Help

jql -h
jql --help

🦀 Workspace

This project is composed of following crates:

Development

Some commands are available as a justfile at the root of the workspace (testing / fuzzing).

Prerequisites

Commands

just --list

⚠️ Non-goal

There's no plan to align jql with jq or any other similar tool.

Performance

Some benchmarks comparing a set of similar functionalities provided by this tool and jq are available here.

📔 Licenses

More Repositories

1

hypergraph

Hypergraph is data structure library to create a directed hypergraph in which a hyperedge can join any number of vertices.
Rust
274
star
2

shrimpit

Shrimpit 🍤 is a small CLI analysis tool for checking unused JavaScript, JSX & Vue templates ES6 exports in your project.
JavaScript
258
star
3

babel-react-rollup-starter

A simple boilerplate for web apps with React, Babel, and Rollup.
JavaScript
121
star
4

craftql

A CLI tool to visualize GraphQL schemas and to output a graph data structure as a graphviz .dot format
Rust
108
star
5

rust-wasm-webpack

A simple boilerplate to get WebAssembly (WASM) code generated by Rust and bundled by Webpack!
JavaScript
88
star
6

buble-react-rollup-starter

A simple starter project to build cool React applications with Bublé and Rollup.
JavaScript
73
star
7

pattern-guard

JavaScript pattern guards 💂
JavaScript
68
star
8

picst

A cross-platform CLI tool to resize clipboard images on the fly
Rust
37
star
9

reflex-starter

A simple starter project for building Reflex applications with Stack.
Shell
36
star
10

Sigma.io

Sigma.io - Create and share data in true real-time
JavaScript
5
star
11

yume

An encrypted peer-to-peer IPv6 UDP messaging terminal client built with Rust
Rust
5
star
12

extend-package-scripts-example

An example of how to extend your package.json scripts by passing extra files or flags as arguments.
JavaScript
4
star
13

docker-flowtype

A Docker image for Flow based on ocaml/opam:debian-8_ocaml-4.03.0
4
star
14

nixos-configuration

Nix
3
star
15

react-vr-typescript

Create amazing 360 and VR content using React & TypeScript
JavaScript
3
star
16

hyperzig

HyperZig - A Hypergraph Implementation in Zig
Zig
2
star
17

git-chart-js

A simple chart rendering Angular app
JavaScript
1
star
18

polymer-game

JavaScript
1
star
19

ruster

A Docker image for Rust based on alpine:edge
1
star
20

yamafaktory

That's me!
1
star
21

.emacs.d

Emacs Lisp
1
star
22

roost

Rust
1
star
23

lumberjack.vim

Vim Script
1
star