• Stars
    star
    584
  • Rank 73,743 (Top 2 %)
  • Language
    Clojure
  • License
    Eclipse Public Li...
  • Created almost 5 years ago
  • Updated 11 months ago

Reviews

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

Repository Details

CLI to transform between JSON, EDN, YAML and Transit using Clojure

CircleCI Clojars Project cljdoc badge

CLI to transform between JSON, EDN, YAML and Transit using Clojure.

Quickstart

$ bash < <(curl -s https://raw.githubusercontent.com/borkdude/jet/master/install)
$ echo '{:a 1}' | jet --to json
{"a":1}

Rationale

This is a command line tool to transform between JSON, EDN and Transit using Clojure. It runs as a GraalVM binary with fast startup time which makes it suited for shell scripting. It may seem familiar to users of jq.

Installation

Brew

Linux and macOS binaries are provided via brew.

Install:

brew install borkdude/brew/jet

Upgrade:

brew upgrade jet

Windows

On Windows you can install using scoop and the scoop-clojure bucket.

Installer script

Install via the installer script:

$ bash <(curl -s https://raw.githubusercontent.com/borkdude/jet/master/install)

By default this will install into /usr/local/bin. To change this, provide the directory name:

$ bash <(curl -s https://raw.githubusercontent.com/borkdude/jet/master/install) /tmp

Download

You may also download a binary from Github.

JVM

Leiningen

This tool can also be used via the JVM. If you use leiningen, you can put the following in your .lein/profiles:

{:user
 {:dependencies [[borkdude/jet "0.4.23"]]
  :aliases {"jet" ["run" "-m" "jet.main"]}}}

And then call jet like:

$ echo '["^ ","~:a",1]' | lein jet --from transit --to edn
{:a 1}

Deps.edn

In deps.edn:

:jet {:deps {borkdude/jet {:mvn/version "0.4.23"}}
      :exec-fn jet.main/exec
      :main-opts ["-m" "jet.main"]}

You can use both the -M and -X style invocation, whichever you prefer:

$ echo '[1 2 3]' | clj -M:jet --colors --func '#(-> % first inc)'
2

$ echo '[1 2 3]' | clj -X:jet :colors true :thread-last '"(map inc)"'
(2 3 4)

Or install jet as a clj tool:

$ clojure -Ttools install-latest :lib io.github.borkdude/jet :as jet

$ echo '[1 2 3]' | clj -Tjet exec :colors true :func '"#(-> % first inc)"'
2

Usage

jet supports the following options:

  -i, --from            [ edn | transit | json | yaml ] defaults to edn.
  -o, --to              [ edn | transit | json | yaml ] defaults to edn.
  -t, --thread-last                                     implicit thread last
  -T, --thread-first                                    implicit thread first
  -f, --func                                            a single-arg Clojure function, or a path to a file that contains a function, that transforms input.
      --no-pretty                                       disable pretty printing
  -k, --keywordize      [ <key-fn> ]                    if present, keywordizes JSON/YAML keys. The default transformation function is keyword unless you provide your own.
      --colors          [ auto | true | false]          use colored output while pretty-printing. Defaults to auto.
      --edn-reader-opts                                 options passed to the EDN reader.
      --no-commas                                       remove commas from EDN
  -c, --collect                                         given separate values, collects them in a vector.
  -h, --help                                            print this help text.
  -v, --version                                         print the current version of jet.
  -q, --query                                           DEPRECATED, prefer -t, -T or -f. Given a jet-lang query, transforms input.

Transform EDN using --thread-last, --thread-first or --func.

Examples:

$ echo '{"a": 1}' | jet --from json --to edn
{"a" 1}

$ echo '{"a": 1}' | jet -i json --keywordize -o edn
{:a 1}

$ echo '{"my key": 1}' | jet -i json -k '#(keyword (str/replace % " " "_"))' -o edn
{:my_key 1}

$ echo '{"anApple": 1}' | jet -i json -k '#(-> % csk/->kebab-case keyword)' -o edn
{:an-apple 1}

$ echo '{"a": 1}' | jet -i json -o yaml
a: 1

$ echo '{"a": 1}' | jet -i json -o transit
["^ ","a",1]

$ echo '{:a {:b {:c 1}}}' | jet --thread-last ':a :b :c'
1

$ echo '{:a {:b {:c 1}}}' | jet --func '#(-> % :a :b :c)'
1

$ echo '{:a {:b {:c [1 2]}}}' | jet -t ':a :b :c (map inc)'
(2 3)

$ cat /tmp/fn.clj
#(-> % :a :b :c)
$ echo '{:a {:b {:c 1}}}' | jet --func /tmp/fn.clj
1

$ echo '{:a {:a 1}}' | ./jet -t '(s/transform [s/MAP-VALS s/MAP-VALS] inc)'
{:a {:a 2}}

Raw output

Get raw output from query rather than wrapped in quotes:

$ echo '{"a": "hello there"}' | jet --from json --keywordize -t ":a" --to edn
"hello there"

$ echo '{"a": "hello there"}' | jet --from json --keywordize -t ":a symbol" --to edn
hello there

or simply use println to get rid of the quotes:

$ echo '{"a": "hello there"}' | jet --from json --keywordize -t ":a println" --to edn
hello there

Data readers

You can enable data readers by passing options to --edn-reader-opts:

$ echo '#foo{:a 1}' | jet --edn-reader-opts '{:default tagged-literal}'
#foo {:a 1}
$ echo '#foo{:a 1}' | jet --edn-reader-opts "{:readers {'foo (fn [x] [:foo x])}}"
[:foo {:a 1}]

See this blog by Alex Miller for more information on the tagged-literal function.

Since jet 0.0.14 --edn-reader-opts defaults to {:default tagged-literal}.

Streaming

Jet supports streaming over multiple values, without reading the entire input into memory:

$ echo '{"a": 1} {"a": 1}' | jet --from json --keywordize -t ':a' --to edn
1
1

When you want to collect multiple values into a vector, you can use --collect:

$ echo '{"a": 1} {"a": 1}' | lein jet --from json --keywordize --collect --to edn
[{:a 1} {:a 1}]

Specter

As of version 0.2.18 the specter library is available in --func, --thread-first and --thread-last:

$ echo '{:a {:a 1}}' | ./jet -t '(s/transform [s/MAP-VALS s/MAP-VALS] inc)'
{:a {:a 2}}

Base64

To encode and decode base64 you can use base64/encode and base64/decode.

Jet utility functions

In the jet namespace, the following utilities are available:

paths

Return all paths (and sub-paths) in maps and vectors. Each result is a map of :path and :val (via get-in).

$ echo '{:a {:b [1 2 3 {:x 2}] :c {:d 3}}}' | jet -t '(jet/paths)'
[{:path [:a], :val {:b [1 2 3 {:x 2}], :c {:d 3}}}
 {:path [:a :b], :val [1 2 3 {:x 2}]}
 {:path [:a :c], :val {:d 3}}
 {:path [:a :b 0], :val 1}
 {:path [:a :b 1], :val 2}
 {:path [:a :b 2], :val 3}
 {:path [:a :b 3], :val {:x 2}}
 {:path [:a :b 3 :x], :val 2}
 {:path [:a :c :d], :val 3}]

when-pred

Given a predicate, return predicate that returns the given argument when predicate was truthy. In case of an exception during the predicate call, catches and returns nil.

The following returns all paths for which the leafs are odd numbers:

$ echo '{:a {:b [1 2 3 {:x 2}] :c {:d 3}}}' | jet -t '(jet/paths) (filter (comp (jet/when-pred odd?) :val)) (mapv :path)'
[[:a :b 0] [:a :b 2] [:a :c :d]]

Emacs integration

Sometimes it's useful to reformat REPL output in Emacs to make it more readable, copy to clipboard or just pretty-print to another buffer. All of that is avaiable in the jet.el package.

Vim integration

To convert data in vim buffers, you can select the data you want to convert in visual mode, then invoke jet by typing for example :'<,'>!jet -k --from json (vim will insert the '<,'> for you as you type).

Test

Test the JVM version:

script/test

Test the native version:

JET_TEST_ENV=native script/test

Build

You will need leiningen and GraalVM.

script/compile

License

Copyright ยฉ 2019-2023 Michiel Borkent

Distributed under the EPL License, same as Clojure. See LICENSE.

More Repositories

1

carve

Remove unused Clojure vars
Clojure
275
star
2

grasp

Grep Clojure code using clojure.spec regexes
Clojure
233
star
3

deps.clj

A faithful port of the clojure CLI bash script to Clojure
Clojure
225
star
4

speculative

Unofficial community-driven specs for clojure.core
Clojure
185
star
5

edamame

Configurable EDN/Clojure parser with location metadata
Clojure
155
star
6

clojure-rust-graalvm

An example of Clojure program calling a Rust library, all combined into one executable using GraalVM.
Rust
120
star
7

re-find

Find functions by matching specs
Clojure
118
star
8

quickblog

Light-weight static blog engine for Clojure and babashka
Clojure
117
star
9

quickdoc

Quick and minimal API doc generation for Clojure
Clojure
116
star
10

bebo

Run Clojure scripts on deno
Clojure
98
star
11

flycheck-clj-kondo

Emacs integration for clj-kondo via flycheck
Emacs Lisp
84
star
12

rewrite-edn

Utility lib on top of rewrite-clj with common operations to update EDN while preserving whitespace and comments.
Clojure
76
star
13

clj2el

Transpile Clojure to Emacs Lisp!
Clojure
63
star
14

glam

A cross-platform package manager.
Clojure
62
star
15

api-diff

Print API diffs between library versions
Clojure
59
star
16

deflet

Make let-expressions REPL-friendly!
Clojure
57
star
17

cljtree-graalvm

Tree version in Clojure built with GraalVM
Clojure
53
star
18

respeced

Testing library for clojure.spec fdefs
Clojure
52
star
19

jayfu

Jayfu is a tutorial on how to create a Clojure CLI with GraalVM native-image and SCI.
Clojure
52
star
20

dynaload

The dynaload logic from clojure.spec.alpha as a library
Clojure
52
star
21

lein2deps

Lein project.clj to deps.edn converter
Clojure
50
star
22

blog

HTML
49
star
23

plsci

PostgreSQL procedural language handler for Clojure via SCI
Rust
48
star
24

advent-of-cljc

Cross platform Clojure Advent of Code solutions
Clojure
44
star
25

boot-bundle

boot-bundle: managed dependencies for boot, the clojure build tool
Clojure
43
star
26

deps-infer

Infer mvn deps from sources
Clojure
39
star
27

puget-cli

A CLI version of puget
Shell
37
star
28

specter-cli

A native Specter CLI, compiled with GraalVM native-image and executed by SCI.
Clojure
33
star
29

draggable-button-in-reagent

Very simple example of a draggable button in Reagent
Clojure
31
star
30

spartan.spec

A spartan version of clojure.spec compatible with babashka
Clojure
30
star
31

lein-new-liberagent

leiningen template for apps that use reagent and liberator
Clojure
29
star
32

re-find.web

Web version of re-find
Clojure
25
star
33

nbb-action-example

An example of writing a Github action with nbb
Clojure
24
star
34

lein2boot

The goal of this exercise is to convert a leiningen project to boot and have exactly the same workflow.
Clojure
24
star
35

gh-release-artifact

Upload artifacts to Github releases idempotently
Clojure
24
star
36

balcony

Should I water my balcony?
Clojure
23
star
37

generate-podcast

A babashka script to create a podcast from a local directory with mp3 files
Clojure
22
star
38

tools

Tools
Clojure
19
star
39

fly_io_clojure

A fly.io example for Clojure
Clojure
17
star
40

refl

Clean up generated reflection configs for GraalVM native-image compiled Clojure programs
Clojure
17
star
41

analyze-reify

Analyze occurrences of reify in Clojure code. Implemented using tree-sitter-clojure and Rust.
Rust
17
star
42

advent-of-babashka

Advent of Code using babashka and nbb
Clojure
16
star
43

aoc2017

Advent of Code 2017
Clojure
15
star
44

lein-new-wrom

Webjars + Ring + Om leiningen template
Clojure
15
star
45

clj-reflector-graal-java11-fix

A fix for an issue with clojure.lang.Reflector in GraalVM native-image JDK11.
Clojure
15
star
46

clj.el

Clojure-like macros and functions in elisp
Emacs Lisp
13
star
47

cljs-showcase

Clojure
13
star
48

balcony-hs

Should I water my balcony?
Haskell
13
star
49

babashka-docker-action-example

Dockerfile
12
star
50

who-follows-me

Check who follows you, but you're not following back and vice versa. https://twitter.michielborkent.nl
Clojure
12
star
51

trickle-playground

A Truffle Clojure Interpreter (playground)
Java
12
star
52

finitize

Limit and realize possibly infinite seqs
Clojure
12
star
53

nrepl-server

Proof of concept nREPL server
Clojure
10
star
54

full-stack-clojure-han-okt-2015

Talk about developing full stack Clojure applications at HAN University of Applied Sciences
Clojure
10
star
55

clojurecursus

Introductiecursus Functioneel Programmeren met Clojure
CSS
10
star
56

missing.test.assertions

A library that detects missing test assertions in clojure.test tests
Clojure
9
star
57

deps.add-lib

Clojure 1.12's add-lib feature for leiningen and/or other environments without a specific version of the clojure CLI
Clojure
8
star
58

figwheel-keep-om-turning

Code for blog post at http://blog.michielborkent.nl/2014/09/25/figwheel-keep-Om-turning/
Clojure
8
star
59

cljtree-planck

A ClojureScript version of `tree` in Planck
Shell
6
star
60

bun-squint-loader

JavaScript
6
star
61

simple-cljs-examples

Curated list of ClojureScript sample applications suited for those starting with ClojureScript
6
star
62

graal.locking

A workaround for CLJ-1472 as a library
Clojure
6
star
63

tictactoe

tictactoe (clojure)
Clojure
5
star
64

bb-cherry-example

A web app showing real time JS compilation with babashka and cherry
Clojure
5
star
65

immutable-webapp

Immutable Webapp!
HTML
5
star
66

spartan.test

A spartan test framework compatible with babashka.
Clojure
5
star
67

speculative-kaocha-plugin

speculative kaocha plugin
Clojure
5
star
68

aoc2015_day7

Solution of Advent of Code Day 7 using clojure.spec
Clojure
4
star
69

oredev2014

Slides and code for ร˜redev 2014
Clojure
4
star
70

tictactoe-cljs

Tictactoe in Clojurescript and Om
Clojure
4
star
71

cherry-action-example

Github action implemented with cherry
Clojure
4
star
72

homebrew-brew

Homebrew formulas
Shell
4
star
73

datalevin-native

Clojure
4
star
74

sci-wallpaper-downloader

A port of @yogthos's wallpaper downloader to the Small Clojure Interpreter on NodeJS
Clojure
4
star
75

domcode-cljs-react

Slides and code for presentation at the DomCode meetup
Clojure
4
star
76

clj-jdbc

A hypothetical Clojure command tool focusing around SQL interaction via JDBC
Clojure
4
star
77

sci-birch

tree CLI using sci, ported from lambdaisland's birch
Clojure
3
star
78

clj-native-sound-demo

A demo of using the Java sound API with GraalVM native-image
Clojure
3
star
79

bb-php-guestbook

A guestbook using babashka and PHP
Clojure
2
star
80

michielborkent.nl

Home page sources
Clojure
2
star
81

clj-kondo-configurator

CLI tool to merge clj-kondo configurations.
Clojure
2
star
82

boot.bundle.edn

My boot.bundle.edn file
Clojure
2
star
83

my-aob

Clojure
2
star
84

FP-AMS-ClojureScript-talk

Slides and code for talk at FP AMS about Clojurescript
Clojure
2
star
85

react-amsterdam

Talk for React-Amsterdam, February 12th 2015
Clojure
2
star
86

pprint

Clojure
2
star
87

sytac-core-async

Slides and code for presentation at Sytac Devjam
Clojure
2
star
88

SnakeYAML

Temporary fork of SnakeYAML
Java
1
star
89

leiningen.org

Leiningen's web site
Clojure
1
star
90

itunes2dropbox

Share the current playing song or selected songs from iTunes to your Dropbox public folder.
AppleScript
1
star
91

try_git

1
star
92

clj-1472-repro

A repro of CLJ-1472
Clojure
1
star
93

cljs-macro

Example of usage of a macro in ClojureScript.
Clojure
1
star
94

squint-bun-cloudflare

JavaScript
1
star
95

aoc2016

Advent of Clojure 2016
Clojure
1
star
96

fp-hu-may-2016

Talk about Functional Programming at Hogeschool Utrecht, May 2016
Clojure
1
star
97

Clojure-FinalAssignment

Quiz
Clojure
1
star
98

ns-parser

Helper library for parsing ns forms in combination with edamame and rewrite-clj
Clojure
1
star
99

test-repo

Repo to test Github features
Clojure
1
star
100

clj-kondo.web

playground for clj-kondo
Clojure
1
star