All Projects → maiha → jq.cr

maiha / jq.cr

Licence: other
thin JSON::Any wrapper to emulate jq for crystal

Programming Languages

crystal
512 projects
Makefile
30231 projects
shell
77523 projects

Labels

Projects that are alternatives of or similar to jq.cr

json to paths
Distill a JSON document into a collection of paths both for 'jq' and 'xpath'
Stars: ✭ 65 (+333.33%)
Mutual labels:  jq
atom-jq
[unmantained] A playground for jq inside atom
Stars: ✭ 22 (+46.67%)
Mutual labels:  jq
api-test
🌿 A simple bash script to test JSON API from terminal in a structured and organized way.
Stars: ✭ 53 (+253.33%)
Mutual labels:  jq
wildq
Command-line TOML/JSON/INI/YAML/XML/HCL processor using jq c bindings
Stars: ✭ 22 (+46.67%)
Mutual labels:  jq
pjs
An awk-like command-line tool for processing text, CSV, JSON, HTML, and XML.
Stars: ✭ 21 (+40%)
Mutual labels:  jq
gosquito
gosquito ("go" + "mosquito") is a pluggable tool for data gathering, data processing and data transmitting to various destinations.
Stars: ✭ 25 (+66.67%)
Mutual labels:  jq
jsqry-cli2
Small CLI tool (similar to jq) to query JSON using sane DSL
Stars: ✭ 21 (+40%)
Mutual labels:  jq
sjq
Command-line JSON processor with Scala syntax
Stars: ✭ 54 (+260%)
Mutual labels:  jq
SciFi Conky HUD
SciFi theme for Conky
Stars: ✭ 33 (+120%)
Mutual labels:  jq
ghrecipes
⛔ ARCHIVED ⛔ Provides some helper functions for using the GitHub V4 API
Stars: ✭ 28 (+86.67%)
Mutual labels:  jq
jqkungfu
A jq playground, written in WebAssembly
Stars: ✭ 108 (+620%)
Mutual labels:  jq
jq-zsh-plugin
jq zsh plugin
Stars: ✭ 155 (+933.33%)
Mutual labels:  jq
vscode-jq
jq LiveView Extension for VS Code
Stars: ✭ 15 (+0%)
Mutual labels:  jq
jqjs
Pure-JavaScript implementation of the jq JSON query language
Stars: ✭ 39 (+160%)
Mutual labels:  jq
onymous-plurk
Anonymous Plurk Cross-matching Tool - Plurk偷偷說交叉比對工具
Stars: ✭ 20 (+33.33%)
Mutual labels:  jq
ycat
Command line processor for YAML/JSON files using Jsonnet
Stars: ✭ 21 (+40%)
Mutual labels:  jq
biowasm
WebAssembly modules for genomics
Stars: ✭ 115 (+666.67%)
Mutual labels:  jq
jfq
JSONata on the command line
Stars: ✭ 29 (+93.33%)
Mutual labels:  jq
jqmd
Write and document your jq/shell scripts with markdown
Stars: ✭ 24 (+60%)
Mutual labels:  jq
cheatsheets
My Cheatsheet Repository
Stars: ✭ 193 (+1186.67%)
Mutual labels:  jq

jq.cr

thin JSON::Any wrapper to emulate jq for Crystal.

see Wiki for examples

breaking changes

  • v0.5.0 : (breaking-change) mapping: parse Time without formats.

crystal versions

  • v0.5.1 : 0.24 or lower
  • v0.6.0 : 0.25 .. 0.27
  • v1.0.0 : 0.27 .. 1.1.1

Usage

  • For example, here is a Grafana request data.
{
  "panelId":1,
  "range":{"from":"2016-09-02T13:32:09.981Z","to":"2016-09-02T14:17:34.306Z"},
  "rangeRaw":{"from":"2016-09-02T13:32:09.981Z","to":"2016-09-02T14:17:34.306Z"},
  "interval":"2s",
  "targets":[{"target":"cpu","refId":"A"},{"target":"mem","refId":"B"}],
  "format":"json",
  "maxDataPoints":1299
}

Parse in Functional way

  • Just call 'Jq#[]` with query path.
require "jq"

jq = Jq.new(str)
jq[".range.from"].as_s       # => "2016-09-02T13:32:09.981Z"
jq[".targets[].target"].as_a # => ["cpu","mem"]
jq[".format"].as_s           # => "json"
jq[".maxDataPoints"].as_i    # => 1299
jq[".xxx"]                   # Jq::NotFound("`.xxx' Missing hash key: "xxx")
jq[".xxx"]?                  # => nil
  • See spec/fixtures/* files for further usage, or try crystal spec -v for full features

Auto parsing and casting by mapping

  • looks like JSON.mapping except this requires Tuple(type, json_path) for its arg.
  • NOTE: use Int64 rather than Int32 for Integer
require "jq"

class Request
  Jq.mapping({
    from:    {Time, ".range.from"},
    targets: {Array(String), ".targets[].target"},
    format:  String,
    max:     {Int64, ".maxDataPoints"},
  })
end

req = Request.from_json(str)
req.from        # => Time.new(2016,9,2,13,32,9,981)
req.targets     # => ["cpu","mem"]
req.format      # => "json"
req.max         # => 1299
req.to_h["max"] # => 1299

req = Request.from_json("{}")
req.max         # Jq::NotFound(key: "max")
req.max?        # => nil

default value

  • override default_XXX to customize the behaviour of missing XXX
require "jq"

class User
  Jq.mapping({
    name: String,
  })

  def default_name
    "(no name)"
  end
end

user = User.from_json("{}")
user.name    # => "(no name)"

custom builder

  • override build_XXX(jq : Jq, hint : String) to customize the behaviour of building XXX

Imagine a strange API that returns a String if null, regardless of the value of Int type.

[
  {"id": 1, "value": 10},
  {"id": 2, "value": "--"}
]

In this case, you can write your own logic to accept it by overriding build_count.

class Report
  Jq.mapping({
    id: Int32,
    count: Int32?,
  })

  def build_count(jq : Jq, hint : String)
    case jq.raw
    when "--"
      nil
    else
      jq.cast(Int32, hint)
    end
  end
end

reports = Array(Report).from_json <<-EOF
  [
    {"id": 1, "count": 10},
    {"id": 2, "count": "--"}
  ]
  EOF
reports.map(&.count?) # => [10, nil]

Installation

Add this to your application's shard.yml:

dependencies:
  jq:
    github: maiha/jq.cr
    version: 1.0.0

CI

Run make test/x.x.x to test crystal-x.x.x. And to test all the versions, run ci.

$ make test/1.1.1
$ ./ci

Contributing

  1. Fork it ( https://github.com/maiha/jq.cr/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request
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].