/y2j

A command line tool for converting between YAML and JSON and vice versa.

Primary LanguageShellMIT LicenseMIT

#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

  • y2j and yq only support the subset of YAML streams that can be losslessly represented in JSON - that is: trees. Graphs, anchors and references are not supported.
  • j2y only supports reading of a single JSON object or a single JSON array from stdin. If the JSON input contains multiple objects, consider using '-s .' with j2y to slurp the input into a single JSON array.
  • yq only supports jq-filters that are guaranteed to produce a single JSON object or array.

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

  • Fixed typo in installation message.
  • Support running on Linux.

Both fixes thanks to reports submitted by Henrik Holmboe.

##1.1

  • Added support for yq.

##1.0

  • Initial release.