json-toml

copy from https://github.com/woodruffw/toml2json

A small CLI to convert between JSON, JSON5, TOML, YAML, and ENV formats — filter the parsed input with a jq query, and/or run a command with that input exposed as environment variables.

Install

cargo install --git https://git.hatter.ink/hatter/json-toml.git

Usage

json-toml [OPTIONS] [INPUT] [-- <EXEC_ARGS>...]

INPUT is a file path, or - for stdin (the default). When no input format flag is given, json-toml auto-detects JSON / JSON5 / TOML / YAML / ENV.

Options

Option Description
-p, --pretty Pretty-print the output
-t, --input-toml Input is TOML
-j, --input-json Input is JSON
-5, --input-json5 Input is JSON5
-e, --input-env Input is ENV (KEY=value lines)
-y, --input-yaml Input is YAML
-T, --output-toml Output TOML
-J, --output-json Output JSON (default)
-Y, --output-yaml Output YAML
--exec Run the command given after -- with the input as env vars
--jq EXPR Filter the input with a jq query before output / --exec

Input and output flags can be combined, e.g. -5Y = JSON5 in → YAML out.

Convert between formats

JSON → TOML:

echo '{"a":1}' | json-toml -jT

JSON5 → YAML:

echo '{a:1}' | json-toml -5Y

Run a command with the input as environment variables

With --exec, the parsed input is no longer printed. Instead, the top-level fields are exposed to the command as environment variables, with each key uppercased (aA, nameNAME). String values are passed through as-is; numbers, booleans, and nested values are stringified. The parent environment is inherited, so existing env vars are still available.

The command and any extra arguments are passed after --:

echo '{a:1,b:2}' | json-toml -5 --exec -- env
# runs `env` with A=1 and B=2 added to the environment
echo '{name:"hatter",count:3,flag:true}' | json-toml -5 --exec -- sh -c 'echo "$NAME $COUNT $FLAG"'
# outputs: hatter 3 true

A file path can be used instead of stdin:

echo '{a:1,b:2}' > config.json5
json-toml -5 --exec config.json5 -- env

Filter the input with a jq query

--jq EXPR runs a jq-style query (via the jaq engine, which covers most of jq's syntax) on the parsed input, after format detection and before output / --exec. Each query result flows downstream independently:

  • Output: each result is printed separately, jq-style. JSON prints one value per line; YAML emits one document per result (--- separated); TOML accepts a single result only (multi-result TOML errors out).
  • --exec: the command runs once per result, with that result's fields as environment variables.

Extract a field:

echo '{"a":1,"b":2}' | json-toml -j --jq '.a'
# 1

Iterate an array (one value per line):

echo '[1,2,3]' | json-toml -j --jq '.[]'
# 1
# 2
# 3

Filter with select and a pipe:

echo '{"users":[{"name":"a","ok":true},{"name":"b","ok":false}]}' \
  | json-toml -j --jq '.users[] | select(.ok) | .name'
# "a"

Combine --jq with --exec to run a command per matched item:

echo '{"users":[{"name":"hatter","ok":true},{"name":"bob","ok":false},{"name":"c","ok":true}]}' \
  | json-toml -j --jq '.users[] | select(.ok)' --exec -- sh -c 'echo "hi $NAME"'
# hi hatter
# hi c
Languages
Rust 100%