https://github.com/nilsding/rj
a Ruby command-line JSON processor
https://github.com/nilsding/rj
json mruby ruby
Last synced: 3 months ago
JSON representation
a Ruby command-line JSON processor
- Host: GitHub
- URL: https://github.com/nilsding/rj
- Owner: nilsding
- License: gpl-3.0
- Created: 2022-07-08T15:40:35.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-06-06T18:57:23.000Z (over 2 years ago)
- Last Synced: 2025-05-05T02:54:33.576Z (8 months ago)
- Topics: json, mruby, ruby
- Language: Ruby
- Homepage: https://nilsding.github.io/rj/
- Size: 160 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# rj
a tool that's like `jq` but uses Ruby expressions
## Usage
```sh
# get the first element of an array (Enumerable#first)
tool_that_emits_json | rj '.first'
# get the 3rd element (Array#[])
tool_that_emits_json | rj '[2]'
# get specific fields (Hash#slice)
tool_that_emits_json | rj '.first.slice("foo", "bar")'
# or map them yourself, the second expression will be applied to the result
# of the first. 'item' is the special variable that holds the previous
# result
# this also demonstrates the usage of Hash#dig to fetch nested elements
tool_that_emits_json | rj '.first' '{ one: item["foo"], two: item.dig("bar", "name") }'
# same as above, but for all elements (Enumerable#map)
tool_that_emits_json | rj '.map { |item| { one: item["foo"], two: item.dig("bar", "name") } }'
# instead of `[]` and `dig` you can access an object's fields like methods
# too.
# this only works with keys that aren't already methods (e.g. `first`, `dig`,
# `slice`, ...), and only on objects -- arrays need to be accessed like '[0]'.
tool_that_emits_json | rj '.map { |item| { one: item.foo, two: item.bar.name } }'
```
## Requirements
for building:
- CMake
- Ruby 2.5 or later
- [whatever else mruby 3.2.0 needs][mruby_deps] :^)
## Building
```sh
# configure it
cmake --preset dev
# compile it
cmake --build --preset dev
# test it
ruby ./test/rj_test.rb
# run it
./build/src/rj
```
[mruby_deps]: https://github.com/mruby/mruby/blob/3.2.0/doc/guides/compile.md#prerequisites