All Projects → markus-wa → cq

markus-wa / cq

Licence: EPL-2.0 license
Clojure Command-line Data Processor for JSON, YAML, EDN, XML and more

Programming Languages

clojure
4091 projects
shell
77523 projects
Batchfile
5799 projects
emacs lisp
2029 projects

Projects that are alternatives of or similar to cq

odin
Data-structure definition/validation/traversal, mapping and serialisation toolkit for Python
Stars: ✭ 24 (-78.38%)
Mutual labels:  yaml, csv, msgpack
Jobfunnel
Scrape job websites into a single spreadsheet with no duplicates.
Stars: ✭ 1,528 (+1276.58%)
Mutual labels:  yaml, csv
Countries States Cities Database
🌍 World countries, states, regions, provinces, cities, towns in JSON, SQL, XML, PLIST, YAML, and CSV. All Countries, States, Cities with ISO2, ISO3, Country Code, Phone Code, Capital, Native Language, Timezones, Latitude, Longitude, Region, Subregion, Flag Emoji, and Currency. #countries #states #cities
Stars: ✭ 1,130 (+918.02%)
Mutual labels:  yaml, csv
Chronicle Wire
A Java Serialisation Library that supports multiple formats
Stars: ✭ 204 (+83.78%)
Mutual labels:  yaml, csv
Countries
World countries in JSON, CSV, XML and Yaml. Any help is welcome!
Stars: ✭ 5,379 (+4745.95%)
Mutual labels:  yaml, csv
Structured Text Tools
A list of command line tools for manipulating structured text data
Stars: ✭ 6,180 (+5467.57%)
Mutual labels:  yaml, csv
Srsly
🦉 Modern high-performance serialization utilities for Python (JSON, MessagePack, Pickle)
Stars: ✭ 189 (+70.27%)
Mutual labels:  yaml, msgpack
luban
你的最佳游戏配置解决方案 {excel, csv, xls, xlsx, json, bson, xml, yaml, lua, unity scriptableobject} => {json, bson, xml, lua, yaml, protobuf(pb), msgpack, flatbuffers, erlang, custom template} data + {c++, java, c#, go(golang), lua, javascript(js), typescript(ts), erlang, rust, gdscript, protobuf schema, flatbuffers schema, custom template} code。
Stars: ✭ 1,660 (+1395.5%)
Mutual labels:  yaml, msgpack
Vscode Data Preview
Data Preview 🈸 extension for importing 📤 viewing 🔎 slicing 🔪 dicing 🎲 charting 📊 & exporting 📥 large JSON array/config, YAML, Apache Arrow, Avro, Parquet & Excel data files
Stars: ✭ 245 (+120.72%)
Mutual labels:  yaml, csv
Mashumaro
Fast and well tested serialization framework on top of dataclasses
Stars: ✭ 208 (+87.39%)
Mutual labels:  yaml, msgpack
Remarshal
Convert between CBOR, JSON, MessagePack, TOML, and YAML
Stars: ✭ 421 (+279.28%)
Mutual labels:  yaml, msgpack
artemis cli
A command-line application for tutors to more productively grade programming excises on ArTEMiS
Stars: ✭ 12 (-89.19%)
Mutual labels:  yaml, csv
Datamodel Code Generator
Pydantic model generator for easy conversion of JSON, OpenAPI, JSON Schema, and YAML data sources.
Stars: ✭ 393 (+254.05%)
Mutual labels:  yaml, csv
Re Txt
converts text-formats from one to another, it is very useful if you want to re-format a json file to yaml, toml to yaml, csv to yaml, ... etc
Stars: ✭ 59 (-46.85%)
Mutual labels:  yaml, csv
Choetl
ETL Framework for .NET / c# (Parser / Writer for CSV, Flat, Xml, JSON, Key-Value, Parquet, Yaml, Avro formatted files)
Stars: ✭ 372 (+235.14%)
Mutual labels:  yaml, csv
Just Dashboard
📊 📋 Dashboards using YAML or JSON files
Stars: ✭ 1,511 (+1261.26%)
Mutual labels:  yaml, csv
Miller
Miller is like awk, sed, cut, join, and sort for name-indexed data such as CSV, TSV, and tabular JSON
Stars: ✭ 4,633 (+4073.87%)
Mutual labels:  csv, data-processing
csvy
Import and Export CSV Data With a YAML Metadata Header
Stars: ✭ 52 (-53.15%)
Mutual labels:  yaml, csv
Python Benedict
dict subclass with keylist/keypath support, I/O shortcuts (base64, csv, json, pickle, plist, query-string, toml, xml, yaml) and many utilities. 📘
Stars: ✭ 204 (+83.78%)
Mutual labels:  yaml, csv
dynamic.yaml
DEPRECATED: YAML-based data transformations
Stars: ✭ 14 (-87.39%)
Mutual labels:  yaml, data-transformation

cq (Clojure Query)

Command-line Data Processor for EDN, YAML, JSON, XML and other data formats.

The joy of Clojure's threading macros, but on the command line!

CI / CD codecov GitHub release (latest SemVer) License FOSSA Status

image

Installation

Homebrew

brew install markus-wa/brew/cq

Manual

  1. Download the latest version for your OS from the releases page.
    • note: you should avoid cq-jvm if possible as these are not GraalVM native images and will be slow to start.
  2. Rename binary to cq
  3. chmod +x cq
  4. Move into a location on $PATH

Rationale

While there are a few similar tools out there (such as jq, jet or babashka), cq tries to resolve some of their shortcomings such as having to learn custom query languages, lacking powerful data transformation libraries or quick and easy (yes I said the e word) handling of many input and output formats.

cq aims to:

  • Not require learning yet another query language - it's just data Clojure!
  • Give the user complete power for writing queries, with as few limitations as possible
  • Provide various input and output formats out of the box
  • Be opinionated, ship useful tools/libraries pre-bundled

Features

  • Supports all elements of Clojure that are supported by SCI
  • Data Formats:
    • EDN
    • YAML
    • JSON
    • XML
    • MsgPack
    • CSV
    • Cognitec's Transit format
    • Text (raw and line-separated)
  • Various reader macros that make writing queries easier
  • Threading macro redirection reduces need for parentheses
  • Coloured output / syntax highlig for EDN output
  • No startup lag thanks to GraalVM native-images
  • Comes batteries-included with the following libraries for transforming nested data structures and utilities

Usage

$ cq --help
cq is a command-line data processor for JSON, YAML, EDN and other data formats that utilises Clojure as it's query language.

Usage: cq [options] [--] QUERY

Examples
  echo '{a: {b: [1, 2, 3]}}' | cq ':a :b (map inc)'

  printf 'http://example.com/some/path' | cq -i text -- '-> str/upper-case (str/split #"/") ->> (map str/reverse)'

Options:
  -i, --in FORMAT                         yaml     Input format: csv, edn, json, lines, msgpack, text, transit, yaml
  -o, --out FORMAT                        edn      Output format: csv, edn, json, lines, msgpack, text, transit, yaml
  -p, --[no-]pretty                                Pretty print output - default is true
  -k, --key-fn FN                         keyword  Function used to transform keys - currently only supported for JSON and CSV
      --yaml-unsafe                                Enables unsafe mode in clj-yaml / SnakeYAML
      --[no-]yaml-keywords                         Turn map keys into keywords in clj-yaml - default is true
      --yaml-max-aliases-for-collections           Sets max aliases for collections in clj-yaml / SnakeYAML
      --yaml-allow-recursive-keys                  Allows recursive keys in clj-yaml / SnakeYAML
      --yaml-allow-duplicate-keys                  Allows duplicate keys in clj-yaml / SnakeYAML
      --yaml-flow-style STYLE             auto     Sets flow style in SnakeYAML: auto, block, flow
      --transit-format-in FORMAT          json     Set the reader type for transit: json, json-verbose, msgpack
      --transit-format-out FORMAT         json     Set the writer type for transit: json, json-verbose, msgpack
  -h, --help

See https://github.com/markus-wa/cq for more information.

By default cq uses thread last (->>) semantics.

$ echo '{"a": {"b": [1, 2, 3]}}' | cq ':a :b (map inc)'
(2 3 4)

Using #| you can use the current value as ..

$ curl -s 'https://api.github.com/repos/markus-wa/cq/commits?per_page=5' | \
cq 'second #| {:author (-> . :commit :author :name) :message (-> . :commit :message) :parents (->> . :parents (map :html_url))}'
{:author "Markus Walther",
 :message "tests: fix coloured output breaking tests",
 :parents ("https://github.com/markus-wa/cq/commit/92ff81edbd6f53f0d20aa5a18ccf6cac53bbe50e")}

There's also a destructuring macro #& to make using (let) easier.

$ printf "http://example.com/some/path" | \
  cq -i text -- '-> (str/split #"/") #& ([protocol _ host] {:protocol protocol :host host})'
{:protocol "http:", :host "example.com"}

#f can be used to simplify creating an anonymous function that returns a value, rather than calls a function.
Also note how m/map-kv is provided by medley.core.

$ echo '{a: {b: 2, c: 3}}' | cq ':a  (m/map-kv #f [%2 %1])'
{2 :b, 3 :c}

Threading Macro Redirection

While things like ->->><?as->cond->! are pretty funny, it can be pretty convenient to just redirect a threading macro when you're working on a simple terminal without paredit.

All threading operators will change the query after that point to their implementation until followed by any other threading operator (no need for parentheses).

Note that threading redirection is currently only supported on the top level, not in nested threading macros.

$ printf "http://example.com/some/path" | \
  cq -i text -- '-> str/upper-case (str/split #"/") ->> (map str/reverse)'
(":PTTH" "" "MOC.ELPMAXE" "EMOS" "HTAP")

Currently supported threading operators for redirection:

  • -> thread first
  • ->> thread last
  • some-> thread some
  • some->> thread some last
  • as-> thread with var name

Included Libraries & Namespace Aliases

Library Namespace Alias Example Query
tolitius/xml-in xml-in.core xml #| (xml/find-all . [:universe :system :solar :planet])
medley medley.core m (m/mak-kv (fn [k v] [v k]))
com.rpl/specter com.rpl.specter s (s/transform [MAP-VALS MAP-VALS] inc)
camel-snake-kebab camel-snake-kebab.core csk csk/->SCREAMING_SNAKE_CASE

Reader Macros

This table explains the different reader macros provided by cq. <f> is the form passed in after the reader macro.

Reader Macro Description Interpolates to Example
#| <f> Use the current value as . ((fn [.] <f>)) #| (< 5 . 10)
#map <f> Map elements of a seq (map (fn [.] <f>)) #map {:a (:a .) :first-child (-> . :children first)}
#& (<d> <f...>) Destructure into vars ((fn [.] (let [<d> .] <f>) #& ({:keys [a b c]} [a b c]})
#f <f> Anonymous function, returns value of f, not evaluation of f #(do <f>) (map-kv #f [%2 %1])

Tips & Tricks

cq is slow!

Pretty printing can be pretty slow with the JSON and EDN libraries we use. one trick is to use cq for querying and jq for formatting.

E.g. this is pretty fast

ca data.yaml | cq -o json --no-pretty | jq

TODO

  • maybe HTML & Parquet support

Acknowledgements

This project takes a lot of inspiration from jq, jet and babashka

License

This project is licensed under the EPL-2.0 License.

FOSSA Status

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].