trinitronx/y2j
A bash tool to convert between YAML & JSON. wildducktheories/y2j fork w/fix https://git.io/v6wPA
453
#NAME y2j, j2y, yq - filters to convert JSON to YAML, YAML to JSON and YAML to YAML.
#SYNOPSIS # convert from YAML to JSON y2j < yaml > json j2y -d < yaml > json
# convert from YAML to JSON with optional trailing jq transformation
y2j {jq-filter} < yaml
# convert from JSON to YAML
j2y < json > yaml
y2j -d < json > yaml
# convert from JSON to YAM with optional leading jq transformation
j2y {jq-filter} < json > yaml
# convert YAML to JSON, run jq, convert back to YAML
yq {jq-filter} < yaml > yaml
# create an installer that will install y2j.sh into /usr/local/bin, then run that script with bash
y2j.sh installer /usr/local/bin | sudo bash
#DESCRIPTION
This package provides filters for transforming JSON to YAML, YAML to JSON and YAML to YAML.
YAML to YAML transformations are performed by applying a jq filter to a JSON transformation of the YAML input stream with y2j and transforming the resulting JSON stream back to YAML with j2y.
The script will use the local instances of jq, python and the required python modules if they exist locally or will use a docker container based on the wildducktheories/y2j image otherwise.
#INSTALLATION
docker run --rm wildducktheories/y2j y2j.sh installer /usr/local/bin | sudo bash
Replace /usr/local/bin with a different directory to specify a different installation location or omit to default to /usr/local/bin.
If the installer fails with complaints about lack of a running docker daemon or failure to find a wildducktheories/y2j image, consider changing sudo bash
to sudo -E bash
so that root inherits the current user's docker environment.
#EXAMPLES ##j2y
echo '{ "foo": [ { "id": 1 } , {"id": 2 }] }' | j2y
yields:
foo:
- id: 1
- id: 2
##j2y - with jq pre-stage
echo '{"foo": "bar"}{"foo": "baz"}' | j2y -s .
yields:
- foo: bar
- foo: baz
##y2j
(
y2j <<EOF
foo:
- id: 1
- id: 2
EOF
) | jq -c .
yields:
{"foo":[{"id":1},{"id":2}]}
##yq
yq '.foo=(.foo[]|select(.id == 1)|[.])' <<EOF
foo:
- id: 1
- id: 2
EOF
yields:
foo:
- id: 1
#LIMITATIONS
Behaviour with inputs or filters that do not satisfy these constraints is not defined.
#AUTHOR
Jon Seymour <jon@wildducktheories.com>
#ACKNOWLEDGMENTS
Conversions used by y2j.sh are based on the commandlinefu scripts found here:
Filtering is implemented with jq. See http://stedolan.github.io/jq/.
#REVISIONS
##1.1.1
Both fixes thanks to reports submitted by Henrik Holmboe.
##1.1
##1.0
docker pull trinitronx/y2j