jf
jf is a jo alternative, A small utility to safely format and print JSON objects in the commandline.
However, unlike jo
, where you build the JSON object by nesting jo
outputs,
jf
works similar to printf
, i.e. it expects the template in YAML format as the first argument, and then the values for the placeholders as subsequent arguments.
For example:
# {"one":1,"two":"2","three":[4,"5"]}
INSTALL
Cargo
As a CLI tool
Or as a library:
Nixpkgs
Binaries
USAGE
OPTIONS
option | help |
---|---|
- | alias for -f - , i.e. read template from stdin |
-- | stop parsing CLI options |
-r, --raw | print the raw rendered value without formatting |
-p, --pretty | pretty print the JSON formatted output |
-y, --yaml | print the output as YAML instead of JSON |
-h, --help | print this help message |
-v, --version | print the version number |
-f, --file | treat the template argument as a file to read from |
TEMPLATE
Template should render into valid YAML. It can contain the following placeholders:
%%
a literal%
character%s
%q
read positional argument%-s
%-q
read stdin%(NAME)s
%(NAME)q
read named value from argument%(NAME=DEFAULT)s
%(NAME=DEFAULT)q
placeholder with default value%(NAME@FILE)s
%(NAME@FILE)q
read default value from file path%(NAME@-)s
%(NAME@-)q
read default value from stdin%(NAME?)s
%(NAME?)q
nullable placeholder that defaults to null%(NAME)?s
%(NAME)?q
optional placeholder that defaults to blank%*s
%*q
expand positional args as array items%*-s
%*-q
expand stdin as array items%**s
%**q
expand positional args as key value pairs%**-s
%**-q
expand stdin as key value pairs%(NAME)*s
%(NAME)*q
expand named args as array items%(NAME)**s
%(NAME)**q
expand named args as key value pairs
Use placeholders with suffix q
for safely quoted JSON string and s
for JSON values
other than string.
RULES
- Pass values for positional placeholders in the same order as in the template.
- Pass values for named placeholders using
NAME=VALUE
syntax. - Pass values for named array items using
NAME=ITEM_N
syntax. - Pass values for named key value pairs using
NAME=KEY_N NAME=VALUE_N
syntax. - Pass values to stdin following the order and separate them with null byte (
\0
). - Use
NAME@FILE
syntax to read from file where FILE can be-
for stdin. - Do not pass positional values after named values.
- To allow merging arrays and objects via expansion, trailing comma after
s
andq
, if any, will be auto removed if no value is passed for the expandable placeholder.
EXAMPLES
# 1
# "1"
# {"one":"1","two":"2","three":"3"}
| |
# [1,2,3]
# {"foo":"bar","biz":"baz"}
# {"str or bool":"true","nullable":null}
# {"1":1,"two":"2","3":3,"four":"4","%":null}
SHELL ALIASES
You can set the following aliases in your shell:
Then you can use them like this:
# "1"
# [1,2,3]
# {"one":1,"two":2,"three":3}
# {"1":2,"3":[4,"5"]}
RUST LIBRARY
let json = match format ;